github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/post-processor/atlas/post-processor.go (about) 1 package atlas 2 3 import ( 4 "fmt" 5 "os" 6 "strconv" 7 "strings" 8 9 "github.com/hashicorp/atlas-go/archive" 10 "github.com/hashicorp/atlas-go/v1" 11 "github.com/hashicorp/packer/common" 12 "github.com/hashicorp/packer/helper/config" 13 "github.com/hashicorp/packer/packer" 14 "github.com/hashicorp/packer/template/interpolate" 15 "github.com/mitchellh/mapstructure" 16 ) 17 18 const ( 19 BuildEnvKey = "ATLAS_BUILD_ID" 20 CompileEnvKey = "ATLAS_COMPILE_ID" 21 ) 22 23 // Artifacts can return a string for this state key and the post-processor 24 // will use automatically use this as the type. The user's value overrides 25 // this if `artifact_type_override` is set to true. 26 const ArtifactStateType = "atlas.artifact.type" 27 28 // Artifacts can return a map[string]string for this state key and this 29 // post-processor will automatically merge it into the metadata for any 30 // uploaded artifact versions. 31 const ArtifactStateMetadata = "atlas.artifact.metadata" 32 33 type Config struct { 34 common.PackerConfig `mapstructure:",squash"` 35 36 Artifact string 37 Type string `mapstructure:"artifact_type"` 38 TypeOverride bool `mapstructure:"artifact_type_override"` 39 Metadata map[string]string 40 41 ServerAddr string `mapstructure:"atlas_url"` 42 Token string 43 44 // This shouldn't ever be set outside of unit tests. 45 Test bool `mapstructure:"test"` 46 47 ctx interpolate.Context 48 user, name string 49 buildId int 50 compileId int 51 } 52 53 type PostProcessor struct { 54 config Config 55 client *atlas.Client 56 } 57 58 func (p *PostProcessor) Configure(raws ...interface{}) error { 59 err := config.Decode(&p.config, &config.DecodeOpts{ 60 Interpolate: true, 61 InterpolateContext: &p.config.ctx, 62 InterpolateFilter: &interpolate.RenderFilter{ 63 Exclude: []string{}, 64 }, 65 }, raws...) 66 if err != nil { 67 return err 68 } 69 70 required := map[string]*string{ 71 "artifact": &p.config.Artifact, 72 "artifact_type": &p.config.Type, 73 } 74 75 var errs *packer.MultiError 76 for key, ptr := range required { 77 if *ptr == "" { 78 errs = packer.MultiErrorAppend( 79 errs, fmt.Errorf("%s must be set", key)) 80 } 81 } 82 83 if errs != nil && len(errs.Errors) > 0 { 84 return errs 85 } 86 87 p.config.user, p.config.name, err = atlas.ParseSlug(p.config.Artifact) 88 if err != nil { 89 return err 90 } 91 92 // If we have a build ID, save it 93 if v := os.Getenv(BuildEnvKey); v != "" { 94 raw, err := strconv.ParseInt(v, 0, 0) 95 if err != nil { 96 return fmt.Errorf( 97 "Error parsing build ID: %s", err) 98 } 99 100 p.config.buildId = int(raw) 101 } 102 103 // If we have a compile ID, save it 104 if v := os.Getenv(CompileEnvKey); v != "" { 105 raw, err := strconv.ParseInt(v, 0, 0) 106 if err != nil { 107 return fmt.Errorf( 108 "Error parsing compile ID: %s", err) 109 } 110 111 p.config.compileId = int(raw) 112 } 113 114 // Build the client 115 p.client = atlas.DefaultClient() 116 if p.config.ServerAddr != "" { 117 p.client, err = atlas.NewClient(p.config.ServerAddr) 118 if err != nil { 119 errs = packer.MultiErrorAppend( 120 errs, fmt.Errorf("Error initializing atlas client: %s", err)) 121 return errs 122 } 123 } 124 if p.config.Token != "" { 125 p.client.Token = p.config.Token 126 } 127 128 if !p.config.Test { 129 // Verify the client 130 if err := p.client.Verify(); err != nil { 131 if err == atlas.ErrAuth { 132 errs = packer.MultiErrorAppend( 133 errs, fmt.Errorf("Error connecting to atlas server, please check your ATLAS_TOKEN env: %s", err)) 134 } else { 135 errs = packer.MultiErrorAppend( 136 errs, fmt.Errorf("Error initializing atlas client: %s", err)) 137 } 138 return errs 139 } 140 } 141 return nil 142 } 143 144 func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { 145 // todo: remove/reword after the migration 146 if p.config.Type == "vagrant.box" { 147 return nil, false, fmt.Errorf("Vagrant-related functionality has been removed from Terraform\n" + 148 "Enterprise into its own product, Vagrant Cloud. For more information see\n" + 149 "https://www.vagrantup.com/docs/vagrant-cloud/vagrant-cloud-migration.html\n" + 150 "Please replace the Atlas post-processor with the Vagrant Cloud post-processor,\n" + 151 "and see https://www.packer.io/docs/post-processors/vagrant-cloud.html for\n" + 152 "more detail.\n") 153 } 154 if _, err := p.client.Artifact(p.config.user, p.config.name); err != nil { 155 if err != atlas.ErrNotFound { 156 return nil, false, fmt.Errorf( 157 "Error finding artifact: %s", err) 158 } 159 160 // Artifact doesn't exist, create it 161 ui.Message(fmt.Sprintf("Creating artifact: %s", p.config.Artifact)) 162 _, err = p.client.CreateArtifact(p.config.user, p.config.name) 163 if err != nil { 164 return nil, false, fmt.Errorf( 165 "Error creating artifact: %s", err) 166 } 167 } 168 169 opts := &atlas.UploadArtifactOpts{ 170 User: p.config.user, 171 Name: p.config.name, 172 Type: p.config.Type, 173 ID: artifact.Id(), 174 Metadata: p.metadata(artifact), 175 BuildID: p.config.buildId, 176 CompileID: p.config.compileId, 177 } 178 179 if fs := artifact.Files(); len(fs) > 0 { 180 var archiveOpts archive.ArchiveOpts 181 182 // We have files. We want to compress/upload them. If we have just 183 // one file, then we use it as-is. Otherwise, we compress all of 184 // them into a single file. 185 var path string 186 if len(fs) == 1 { 187 path = fs[0] 188 } else { 189 path = longestCommonPrefix(fs) 190 if path == "" { 191 return nil, false, fmt.Errorf( 192 "No common prefix for archiving files: %v", fs) 193 } 194 195 // Modify the archive options to only include the files 196 // that are in our file list. 197 include := make([]string, len(fs)) 198 for i, f := range fs { 199 include[i] = strings.Replace(f, path, "", 1) 200 } 201 archiveOpts.Include = include 202 } 203 204 r, err := archive.CreateArchive(path, &archiveOpts) 205 if err != nil { 206 return nil, false, fmt.Errorf( 207 "Error archiving artifact: %s", err) 208 } 209 defer r.Close() 210 211 opts.File = r 212 opts.FileSize = r.Size 213 } 214 215 ui.Message(fmt.Sprintf("Uploading artifact (%d bytes)", opts.FileSize)) 216 var av *atlas.ArtifactVersion 217 doneCh := make(chan struct{}) 218 errCh := make(chan error, 1) 219 go func() { 220 var err error 221 av, err = p.client.UploadArtifact(opts) 222 if err != nil { 223 errCh <- err 224 return 225 } 226 close(doneCh) 227 }() 228 229 select { 230 case err := <-errCh: 231 return nil, false, fmt.Errorf("Error uploading (%d bytes): %s", opts.FileSize, err) 232 case <-doneCh: 233 } 234 235 return &Artifact{ 236 Name: p.config.Artifact, 237 Type: p.config.Type, 238 Version: av.Version, 239 }, true, nil 240 } 241 242 func (p *PostProcessor) metadata(artifact packer.Artifact) map[string]string { 243 var metadata map[string]string 244 metadataRaw := artifact.State(ArtifactStateMetadata) 245 if metadataRaw != nil { 246 if err := mapstructure.Decode(metadataRaw, &metadata); err != nil { 247 panic(err) 248 } 249 } 250 251 if p.config.Metadata != nil { 252 // If we have no extra metadata, just return as-is 253 if metadata == nil { 254 return p.config.Metadata 255 } 256 257 // Merge the metadata 258 for k, v := range p.config.Metadata { 259 metadata[k] = v 260 } 261 } 262 263 return metadata 264 } 265 266 func (p *PostProcessor) artifactType(artifact packer.Artifact) string { 267 if !p.config.TypeOverride { 268 if v := artifact.State(ArtifactStateType); v != nil { 269 return v.(string) 270 } 271 } 272 273 return p.config.Type 274 }