aci.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package azure
  2. import (
  3. "bufio"
  4. "context"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "os"
  9. "os/signal"
  10. "runtime"
  11. "strings"
  12. "github.com/docker/api/context/store"
  13. "github.com/gobwas/ws"
  14. "github.com/gobwas/ws/wsutil"
  15. "github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2018-10-01/containerinstance"
  16. "github.com/Azure/azure-sdk-for-go/services/keyvault/auth"
  17. "github.com/Azure/go-autorest/autorest"
  18. "github.com/Azure/go-autorest/autorest/to"
  19. tm "github.com/buger/goterm"
  20. )
  21. func init() {
  22. // required to get auth.NewAuthorizerFromCLI() to work, otherwise getting "The access token has been obtained for wrong audience or resource 'https://vault.azure.net'."
  23. err := os.Setenv("AZURE_KEYVAULT_RESOURCE", "https://management.azure.com")
  24. if err != nil {
  25. panic("unable to set environment variable AZURE_KEYVAULT_RESOURCE")
  26. }
  27. }
  28. func createACIContainers(ctx context.Context, aciContext store.AciContext, groupDefinition containerinstance.ContainerGroup) (c containerinstance.ContainerGroup, err error) {
  29. containerGroupsClient, err := getContainerGroupsClient(aciContext.SubscriptionID)
  30. if err != nil {
  31. return c, fmt.Errorf("cannot get container group client: %v", err)
  32. }
  33. // Check if the container group already exists
  34. _, err = containerGroupsClient.Get(ctx, aciContext.ResourceGroup, *groupDefinition.Name)
  35. if err != nil {
  36. if err, ok := err.(autorest.DetailedError); ok {
  37. if err.StatusCode != http.StatusNotFound {
  38. return c, err
  39. }
  40. } else {
  41. return c, err
  42. }
  43. } else {
  44. return c, fmt.Errorf("container group %q already exists", *groupDefinition.Name)
  45. }
  46. future, err := containerGroupsClient.CreateOrUpdate(
  47. ctx,
  48. aciContext.ResourceGroup,
  49. *groupDefinition.Name,
  50. groupDefinition,
  51. )
  52. if err != nil {
  53. return c, err
  54. }
  55. err = future.WaitForCompletionRef(ctx, containerGroupsClient.Client)
  56. if err != nil {
  57. return c, err
  58. }
  59. containerGroup, err := future.Result(containerGroupsClient)
  60. if err != nil {
  61. return c, err
  62. }
  63. if len(*containerGroup.Containers) > 1 {
  64. var commands []string
  65. for _, container := range *containerGroup.Containers {
  66. commands = append(commands, fmt.Sprintf("echo 127.0.0.1 %s >> /etc/hosts", *container.Name))
  67. }
  68. commands = append(commands, "exit")
  69. containers := *containerGroup.Containers
  70. container := containers[0]
  71. response, err := execACIContainer(ctx, "/bin/sh", *containerGroup.Name, *container.Name, aciContext)
  72. if err != nil {
  73. return c, err
  74. }
  75. err = execWebSocketLoopWithCmd(
  76. ctx,
  77. *response.WebSocketURI,
  78. *response.Password,
  79. commands,
  80. false)
  81. if err != nil {
  82. return containerinstance.ContainerGroup{}, err
  83. }
  84. }
  85. return containerGroup, err
  86. }
  87. func listACIContainers(aciContext store.AciContext) (c []containerinstance.ContainerGroup, err error) {
  88. ctx := context.TODO()
  89. containerGroupsClient, err := getContainerGroupsClient(aciContext.SubscriptionID)
  90. if err != nil {
  91. return c, fmt.Errorf("cannot get container group client: %v", err)
  92. }
  93. var containers []containerinstance.ContainerGroup
  94. result, err := containerGroupsClient.ListByResourceGroup(ctx, aciContext.ResourceGroup)
  95. if err != nil {
  96. return []containerinstance.ContainerGroup{}, err
  97. }
  98. for result.NotDone() {
  99. containers = append(containers, result.Values()...)
  100. if err := result.NextWithContext(ctx); err != nil {
  101. return []containerinstance.ContainerGroup{}, err
  102. }
  103. }
  104. return containers, err
  105. }
  106. func execACIContainer(ctx context.Context, command, containerGroup string, containerName string, aciContext store.AciContext) (c containerinstance.ContainerExecResponse, err error) {
  107. containerClient := getContainerClient(aciContext.SubscriptionID)
  108. rows, cols := getTermSize()
  109. containerExecRequest := containerinstance.ContainerExecRequest{
  110. Command: to.StringPtr(command),
  111. TerminalSize: &containerinstance.ContainerExecRequestTerminalSize{
  112. Rows: rows,
  113. Cols: cols,
  114. },
  115. }
  116. return containerClient.ExecuteCommand(
  117. ctx,
  118. aciContext.ResourceGroup,
  119. containerGroup,
  120. containerName,
  121. containerExecRequest)
  122. }
  123. func getTermSize() (*int32, *int32) {
  124. rows := tm.Height()
  125. cols := tm.Width()
  126. return to.Int32Ptr(int32(rows)), to.Int32Ptr(int32(cols))
  127. }
  128. func execWebSocketLoop(ctx context.Context, wsURL, passwd string) error {
  129. return execWebSocketLoopWithCmd(ctx, wsURL, passwd, []string{}, true)
  130. }
  131. func execWebSocketLoopWithCmd(ctx context.Context, wsURL, passwd string, commands []string, outputEnabled bool) error {
  132. ctx, cancel := context.WithCancel(ctx)
  133. conn, _, _, err := ws.DefaultDialer.Dial(ctx, wsURL)
  134. if err != nil {
  135. cancel()
  136. return err
  137. }
  138. err = wsutil.WriteClientMessage(conn, ws.OpText, []byte(passwd))
  139. if err != nil {
  140. cancel()
  141. return err
  142. }
  143. lastCommandLen := 0
  144. done := make(chan struct{})
  145. go func() {
  146. defer close(done)
  147. for {
  148. msg, _, err := wsutil.ReadServerData(conn)
  149. if err != nil {
  150. if err != io.EOF {
  151. fmt.Printf("read error: %s\n", err)
  152. }
  153. return
  154. }
  155. lines := strings.Split(string(msg), "\n")
  156. lastCommandLen = len(lines[len(lines)-1])
  157. if outputEnabled {
  158. fmt.Printf("%s", msg)
  159. }
  160. }
  161. }()
  162. interrupt := make(chan os.Signal, 1)
  163. signal.Notify(interrupt, os.Interrupt)
  164. scanner := bufio.NewScanner(os.Stdin)
  165. rc := make(chan string, 10)
  166. if len(commands) > 0 {
  167. for _, command := range commands {
  168. rc <- command
  169. }
  170. }
  171. go func() {
  172. for {
  173. if !scanner.Scan() {
  174. close(done)
  175. cancel()
  176. fmt.Println("exiting...")
  177. break
  178. }
  179. t := scanner.Text()
  180. rc <- t
  181. cleanLastCommand(lastCommandLen)
  182. }
  183. }()
  184. for {
  185. select {
  186. case <-done:
  187. return nil
  188. case line := <-rc:
  189. err = wsutil.WriteClientMessage(conn, ws.OpText, []byte(line+"\n"))
  190. if err != nil {
  191. fmt.Println("write: ", err)
  192. return nil
  193. }
  194. case <-interrupt:
  195. fmt.Println("interrupted...")
  196. close(done)
  197. cancel()
  198. return nil
  199. }
  200. }
  201. }
  202. func cleanLastCommand(lastCommandLen int) {
  203. tm.MoveCursorUp(1)
  204. tm.MoveCursorForward(lastCommandLen)
  205. if runtime.GOOS != "windows" {
  206. for i := 0; i < tm.Width(); i++ {
  207. _, _ = tm.Print(" ")
  208. }
  209. tm.MoveCursorUp(1)
  210. }
  211. tm.Flush()
  212. }
  213. func getContainerGroupsClient(subscriptionID string) (containerinstance.ContainerGroupsClient, error) {
  214. auth, _ := auth.NewAuthorizerFromCLI()
  215. containerGroupsClient := containerinstance.NewContainerGroupsClient(subscriptionID)
  216. containerGroupsClient.Authorizer = auth
  217. return containerGroupsClient, nil
  218. }
  219. func getContainerClient(subscriptionID string) containerinstance.ContainerClient {
  220. auth, _ := auth.NewAuthorizerFromCLI()
  221. containerClient := containerinstance.NewContainerClient(subscriptionID)
  222. containerClient.Authorizer = auth
  223. return containerClient
  224. }