Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "golang.org/x/net/context"
  5. "github.com/docker/docker/api/types"
  6. "github.com/docker/docker/api/types/container"
  7. "github.com/docker/docker/api/types/network"
  8. "github.com/docker/docker/cli/command"
  9. "github.com/docker/docker/client"
  10. "fmt"
  11. "os"
  12. )
  13.  
  14. var (
  15. err error
  16. appName string
  17. )
  18.  
  19. // Keep constants with sensitive information in one place
  20. const (
  21. DefaultServer = "registry.gitlab.com"
  22. Username = "xxxxx"
  23. Password = "xxxxx"
  24. )
  25.  
  26. // App struct holds information about the app that should be launched
  27. // with auth and docker client info
  28. type App struct {
  29. Name string
  30. Client *client.Client
  31. AuthConfig types.AuthConfig
  32. AuthConfigEncoded string
  33. Config container.ContainerCreateCreatedBody
  34. }
  35.  
  36. func registryAuthentication(image string) types.RequestPrivilegeFunc {
  37. return func() (string, error) {
  38. authConfig := types.AuthConfig{
  39. Username: Username,
  40. Password: Password,
  41. ServerAddress: DefaultServer,
  42. }
  43. base64config, err := command.EncodeAuthToBase64(authConfig)
  44. if err != nil {
  45. return "", err
  46. }
  47. return base64config, nil
  48. }
  49. }
  50.  
  51. // Initialize application from environment variable
  52. func (app *App) Initialize() error {
  53. app.Client, err = client.NewEnvClient()
  54. return err
  55. }
  56.  
  57. // Prepare auth registry for usage
  58. func (app *App) PrepareRegistry() error {
  59. app.AuthConfig = types.AuthConfig{
  60. Username: Username,
  61. Password: Password,
  62. ServerAddress: DefaultServer,
  63. }
  64.  
  65. resp, err := app.Client.RegistryLogin(context.Background(), app.AuthConfig)
  66. if err != nil {
  67. panic(err)
  68. }
  69.  
  70. fmt.Println("Status:\t", resp.Status)
  71. if resp.IdentityToken != "" {
  72. app.AuthConfig.IdentityToken = resp.IdentityToken
  73. }
  74.  
  75. app.AuthConfigEncoded, err = command.EncodeAuthToBase64(app.AuthConfig)
  76. return err
  77. }
  78.  
  79. func (app *App) ImagePull() error {
  80.  
  81. opts := types.ImagePullOptions{
  82. RegistryAuth: app.AuthConfigEncoded,
  83. PrivilegeFunc: registryAuthentication(app.Name),
  84. }
  85. _, err := app.Client.ImagePull(context.Background(), DefaultServer + "magnus/atom", opts)
  86. if err != nil {
  87. return err
  88. }
  89. return nil
  90. }
  91.  
  92. func (app *App) ImageIsLoaded() bool {
  93. result, err := app.Client.ImageSearch(context.Background(), app.Name, types.ImageSearchOptions{Limit: 1})
  94.  
  95. if err != nil {
  96. panic(err)
  97. }
  98.  
  99. if len(result) != 0 {
  100. return true
  101. }
  102. return false
  103. }
  104.  
  105. // Create config for container
  106. func (app *App) ContainerCreate() error {
  107. // Wait for image to be pulled
  108. for !app.ImageIsLoaded() {
  109. app.Config, err = app.Client.ContainerCreate(
  110. context.Background(),
  111. &container.Config{Image: app.Name},
  112. &container.HostConfig{},
  113. &network.NetworkingConfig{},
  114. "")
  115. return err
  116. }
  117. return nil
  118. }
  119.  
  120. // Launch container with the `App.Name`
  121. func (app *App) ContainerStart() error {
  122. return app.Client.ContainerStart(context.Background(), app.Config.ID, types.ContainerStartOptions{})
  123. }
  124.  
  125. // Launch app
  126. func (app *App) Start() {
  127.  
  128. err = app.Initialize()
  129. if err != nil {
  130. panic(err)
  131. }
  132.  
  133. err = app.PrepareRegistry()
  134. if err != nil {
  135. panic(err)
  136. }
  137.  
  138. err = app.ImagePull()
  139. if err != nil {
  140. panic(err)
  141. }
  142.  
  143. err = app.ContainerCreate()
  144. if err != nil {
  145. panic(err)
  146. }
  147.  
  148. err = app.ContainerStart()
  149. if err != nil {
  150. panic(err)
  151. }
  152.  
  153. }
  154.  
  155. func main() {
  156. if len(os.Args) > 1 {
  157. appName = os.Args[1]
  158. app := App{Name: "magnus/" + appName}
  159. app.Start()
  160. fmt.Sprintln("Launched", appName)
  161. } else {
  162. fmt.Println("Please, provide app name")
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement