github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/examples/container/build/main.go (about) 1 package main 2 3 import ( 4 "archive/tar" 5 "bufio" 6 "compress/gzip" 7 "encoding/json" 8 "flag" 9 "fmt" 10 "io" 11 "io/ioutil" 12 "log" 13 "os" 14 "path" 15 "path/filepath" 16 "strings" 17 18 bluemix "github.com/IBM-Cloud/bluemix-go" 19 "github.com/IBM-Cloud/bluemix-go/session" 20 21 "github.com/IBM-Cloud/bluemix-go/api/container/registryv1" 22 "github.com/IBM-Cloud/bluemix-go/api/iam/iamv1" 23 "github.com/IBM-Cloud/bluemix-go/trace" 24 ) 25 26 func tarGzContext(context string) (string, error) { 27 tarfile, err := ioutil.TempFile("", "docker-*.tar.gz") 28 if err != nil { 29 return "", err 30 } 31 defer tarfile.Close() 32 gw := gzip.NewWriter(tarfile) 33 defer gw.Close() 34 35 tarball := tar.NewWriter(gw) 36 defer tarball.Close() 37 38 info, err := os.Stat(context) 39 if err != nil { 40 return "", err 41 } 42 43 var baseDir string 44 if info.IsDir() { 45 baseDir = filepath.Base(context) 46 } 47 48 err = filepath.Walk(context, 49 func(location string, info os.FileInfo, err error) error { 50 if err != nil { 51 return err 52 } 53 header, err := tar.FileInfoHeader(info, info.Name()) 54 if err != nil { 55 return err 56 } 57 58 if baseDir != "" { 59 header.Name = filepath.Join(path.Clean(baseDir), path.Clean(strings.TrimPrefix(location, context))) 60 } 61 62 if err := tarball.WriteHeader(header); err != nil { 63 return err 64 } 65 66 if info.IsDir() { 67 return nil 68 } 69 70 file, err := os.Open(location) 71 if err != nil { 72 return err 73 } 74 defer file.Close() 75 _, err = io.Copy(tarball, file) 76 return err 77 }) 78 if err != nil { 79 return "", err 80 } 81 return tarfile.Name(), err 82 } 83 84 //Example: ./build -f Dockerfile -t registry.ng.bluemix.net/ibmcloud-go/imagtest .") 85 func main() { 86 87 c := new(bluemix.Config) 88 89 var imageTag string 90 // should same form of registry.ng.bluemix.net/<namespace>/<imagename> 91 flag.StringVar(&imageTag, "t", "registry.ng.bluemix.net/ibmcloud-go/build", "tag") 92 93 var dockerFile string 94 flag.StringVar(&dockerFile, "f", "Dockerfile", "Dockerfile") 95 96 var region string 97 flag.StringVar(®ion, "region", "us-south", "region") 98 99 flag.Parse() 100 101 c.Region = region 102 directory := flag.Args() 103 104 fmt.Println(directory) 105 trace.Logger = trace.NewLogger("true") 106 if len(directory) < 1 || directory[0] == "" { 107 108 flag.Usage() 109 fmt.Println("Example: ./build -f Dockerfile -t registry.ng.bluemix.net/ibmcloud-go/imagtest .") 110 os.Exit(1) 111 } 112 113 session, _ := session.New(c) 114 115 iamAPI, err := iamv1.New(session) 116 identityAPI := iamAPI.Identity() 117 userInfo, err := identityAPI.UserInfo() 118 if err != nil { 119 log.Fatal(err) 120 } 121 registryClient, err := registryv1.New(session) 122 if err != nil { 123 log.Fatal(err) 124 } 125 126 namespaceHeaderStruct := registryv1.NamespaceTargetHeader{ 127 AccountID: userInfo.Account.Bss, 128 } 129 130 namespace := strings.Split(imageTag, "/") 131 if len(namespace) != 3 { 132 log.Fatal("Image Tag not correct format") 133 } 134 135 namespaces, err := registryClient.Namespaces().GetNamespaces(namespaceHeaderStruct) 136 found := false 137 for _, a := range namespaces { 138 if a == namespace[1] { 139 found = true 140 break 141 } 142 } 143 if !found { 144 _, err := registryClient.Namespaces().AddNamespace(namespace[1], namespaceHeaderStruct) 145 if err != nil { 146 log.Fatal(err) 147 } 148 149 } 150 151 headerStruct := registryv1.BuildTargetHeader{ 152 AccountID: userInfo.Account.Bss, 153 } 154 155 requestStruct := registryv1.DefaultImageBuildRequest() 156 requestStruct.T = imageTag 157 requestStruct.Dockerfile = dockerFile 158 159 tarName, err := tarGzContext(directory[0]) 160 if err != nil { 161 162 } 163 tarFile, err := os.Open(tarName) 164 if err != nil { 165 log.Fatal(err) 166 } 167 168 tarReader := bufio.NewReader(tarFile) 169 //Too much binary output 170 trace.Logger = trace.NewLogger("false") 171 172 fmt.Println("Building...") 173 err = registryClient.Builds().ImageBuild(*requestStruct, tarReader, headerStruct, os.Stdout) 174 175 imageHeaderStruct := registryv1.ImageTargetHeader{ 176 AccountID: userInfo.Account.Bss, 177 } 178 179 fmt.Println("\nInspecting Built Image...") 180 image, err := registryClient.Images().InspectImage(imageTag, imageHeaderStruct) 181 if err != nil { 182 log.Fatal(err) 183 } 184 jsonBytes, err := json.MarshalIndent(image, "", " ") 185 if err != nil { 186 log.Fatal(err) 187 } 188 189 fmt.Printf(string(jsonBytes)) 190 191 fmt.Println("\nScanning Built Image...") 192 imageVulnerabiliyRequst := registryv1.DefaultImageVulnerabilitiesRequest() 193 imageReport, err := registryClient.Images().ImageVulnerabilities(imageTag, *imageVulnerabiliyRequst, imageHeaderStruct) 194 if err != nil { 195 log.Fatal(err) 196 } 197 jsonBytes, err = json.MarshalIndent(imageReport, "", " ") 198 if err != nil { 199 log.Fatal(err) 200 } 201 202 fmt.Printf(string(jsonBytes)) 203 }