docker.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. package docker
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "goseg/config"
  7. "goseg/structs"
  8. "strings"
  9. "time"
  10. "github.com/docker/docker/api/types"
  11. "github.com/docker/docker/api/types/container"
  12. "github.com/docker/docker/client"
  13. )
  14. var (
  15. EventBus = make(chan structs.Event, 100)
  16. )
  17. // return the container status of a slice of ships
  18. func GetShipStatus(patps []string) (map[string]string, error) {
  19. statuses := make(map[string]string)
  20. cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
  21. if err != nil {
  22. errmsg := fmt.Sprintf("Error getting Docker info: %v", err)
  23. config.Logger.Error(errmsg)
  24. return statuses, err
  25. } else {
  26. containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{All: true})
  27. if err != nil {
  28. errmsg := fmt.Sprintf("Error getting containers: %v", err)
  29. config.Logger.Error(errmsg)
  30. return statuses, err
  31. } else {
  32. for _, pier := range patps {
  33. found := false
  34. for _, container := range containers {
  35. for _, name := range container.Names {
  36. fasPier := "/" + pier
  37. if name == fasPier {
  38. statuses[pier] = container.Status
  39. found = true
  40. break
  41. }
  42. }
  43. if found {
  44. break
  45. }
  46. }
  47. if !found {
  48. statuses[pier] = "not found"
  49. }
  50. }
  51. }
  52. return statuses, nil
  53. }
  54. }
  55. // return the name of a container's network
  56. func GetContainerNetwork(name string) (string, error) {
  57. cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
  58. if err != nil {
  59. return "", err
  60. }
  61. defer cli.Close()
  62. containerJSON, err := cli.ContainerInspect(context.Background(), name)
  63. if err != nil {
  64. return "", err
  65. }
  66. for networkName := range containerJSON.NetworkSettings.Networks {
  67. return networkName, nil
  68. }
  69. return "", fmt.Errorf("container is not attached to any network")
  70. }
  71. // return the disk and memory usage for a container
  72. func GetContainerStats(containerName string) (structs.ContainerStats, error) {
  73. var res structs.ContainerStats
  74. cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
  75. if err != nil {
  76. return res, err
  77. }
  78. defer cli.Close()
  79. statsResp, err := cli.ContainerStats(context.Background(), containerName, false)
  80. if err != nil {
  81. return res, err
  82. }
  83. defer statsResp.Body.Close()
  84. var stat types.StatsJSON
  85. if err := json.NewDecoder(statsResp.Body).Decode(&stat); err != nil {
  86. return res, err
  87. }
  88. memUsage := stat.MemoryStats.Usage
  89. inspectResp, err := cli.ContainerInspect(context.Background(), containerName)
  90. if err != nil {
  91. return res, err
  92. }
  93. diskUsage := int64(0)
  94. if inspectResp.SizeRw != nil {
  95. diskUsage = *inspectResp.SizeRw
  96. }
  97. return structs.ContainerStats{
  98. MemoryUsage: memUsage,
  99. DiskUsage: diskUsage,
  100. }, nil
  101. }
  102. // start a container by name + type
  103. // contructs a container.Config, then runs through whether to boot/restart/etc
  104. // saves the current container state in memory after completion
  105. func StartContainer(containerName string, containerType string) (structs.ContainerState, error) {
  106. // bundle of info about container
  107. var containerState structs.ContainerState
  108. // config params for container
  109. var containerConfig container.Config
  110. // host config for container
  111. var hostConfig container.HostConfig
  112. // switch on containerType to process containerConfig
  113. switch containerType {
  114. case "vere":
  115. // containerConfig, HostConfig, err := urbitContainerConf(containerName)
  116. _, _, err := urbitContainerConf(containerName)
  117. if err != nil {
  118. return containerState, err
  119. }
  120. case "netdata":
  121. _, _, err := netdataContainerConf()
  122. if err != nil {
  123. return containerState, err
  124. }
  125. case "minio":
  126. _, _, err := minioContainerConf(containerName)
  127. if err != nil {
  128. return containerState, err
  129. }
  130. case "miniomc":
  131. _, _, err := mcContainerConf()
  132. if err != nil {
  133. return containerState, err
  134. }
  135. case "wireguard":
  136. _, _, err := wgContainerConf()
  137. if err != nil {
  138. return containerState, err
  139. }
  140. default:
  141. errmsg := fmt.Errorf("Unrecognized container type %s", containerType)
  142. return containerState, errmsg
  143. }
  144. ctx := context.Background()
  145. cli, err := client.NewClientWithOpts(client.FromEnv)
  146. if err != nil {
  147. return containerState, err
  148. }
  149. // get the desired tag and hash from config
  150. containerInfo, err := GetLatestContainerInfo(containerType)
  151. if err != nil {
  152. return containerState, err
  153. }
  154. // check if container exists
  155. containers, err := cli.ContainerList(ctx, types.ContainerListOptions{All: true})
  156. if err != nil {
  157. return containerState, err
  158. }
  159. var existingContainer *types.Container = nil
  160. for _, container := range containers {
  161. for _, name := range container.Names {
  162. if name == "/"+containerName {
  163. existingContainer = &container
  164. break
  165. }
  166. }
  167. if existingContainer != nil {
  168. break
  169. }
  170. }
  171. desiredImage := fmt.Sprintf("%s:%s@sha256:%s", containerInfo["repo"], containerInfo["tag"], containerInfo["hash"])
  172. desiredStatus := "running"
  173. // check if the desired image is available locally
  174. images, err := cli.ImageList(ctx, types.ImageListOptions{})
  175. if err != nil {
  176. return containerState, err
  177. }
  178. imageExistsLocally := false
  179. for _, img := range images {
  180. if img.ID == containerInfo["hash"] {
  181. imageExistsLocally = true
  182. break
  183. }
  184. if imageExistsLocally {
  185. break
  186. }
  187. }
  188. if !imageExistsLocally {
  189. // pull the image if it doesn't exist locally
  190. _, err = cli.ImagePull(ctx, desiredImage, types.ImagePullOptions{})
  191. if err != nil {
  192. return containerState, err
  193. }
  194. }
  195. switch {
  196. case existingContainer == nil:
  197. // if the container does not exist, create and start it
  198. _, err := cli.ContainerCreate(ctx, &containerConfig, nil, nil, nil, containerName)
  199. if err != nil {
  200. return containerState, err
  201. }
  202. err = cli.ContainerStart(ctx, containerName, types.ContainerStartOptions{})
  203. if err != nil {
  204. return containerState, err
  205. }
  206. msg := fmt.Sprintf("%s started with image %s", containerName, desiredImage)
  207. config.Logger.Info(msg)
  208. case existingContainer.State == "exited":
  209. // if the container exists but is stopped, start it
  210. err := cli.ContainerStart(ctx, containerName, types.ContainerStartOptions{})
  211. if err != nil {
  212. return containerState, err
  213. }
  214. msg := fmt.Sprintf("Started stopped container %s", containerName)
  215. config.Logger.Info(msg)
  216. default:
  217. // if container is running, check the image digest
  218. currentImage := existingContainer.Image
  219. digestParts := strings.Split(currentImage, "@sha256:")
  220. currentDigest := ""
  221. if len(digestParts) > 1 {
  222. currentDigest = digestParts[1]
  223. }
  224. if currentDigest != containerInfo["hash"] {
  225. // if the hashes don't match, recreate the container with the new one
  226. err := cli.ContainerRemove(ctx, containerName, types.ContainerRemoveOptions{Force: true})
  227. if err != nil {
  228. return containerState, err
  229. }
  230. _, err = cli.ContainerCreate(ctx, &container.Config{
  231. Image: desiredImage,
  232. }, nil, nil, nil, containerName)
  233. if err != nil {
  234. return containerState, err
  235. }
  236. err = cli.ContainerStart(ctx, containerName, types.ContainerStartOptions{})
  237. if err != nil {
  238. return containerState, err
  239. }
  240. msg := fmt.Sprintf("Restarted %s with image %s", containerName, desiredImage)
  241. config.Logger.Info(msg)
  242. }
  243. }
  244. containerDetails, err := cli.ContainerInspect(ctx, containerName)
  245. if err != nil {
  246. return containerState, fmt.Errorf("failed to inspect container %s: %v", containerName, err)
  247. }
  248. // save the current state of the container in memory for reference
  249. containerState = structs.ContainerState{
  250. ID: containerDetails.ID, // container id hash
  251. Name: containerName, // name (eg @p)
  252. Image: desiredImage, // full repo:tag@hash string
  253. Type: containerType, // eg `vere` (corresponds with version server label)
  254. DesiredStatus: desiredStatus, // what the user sets
  255. ActualStatus: containerDetails.State.Status, // what the daemon reports
  256. CreatedAt: containerDetails.Created, // this is a string
  257. Config: containerConfig, // container.Config struct constructed above
  258. Host: hostConfig, // host.Config struct constructed above
  259. }
  260. return containerState, err
  261. }
  262. // convert the version info back into json then a map lol
  263. // so we can easily get the correct repo/release channel/tag/hash
  264. func GetLatestContainerInfo(containerType string) (map[string]string, error) {
  265. var res map[string]string
  266. arch := config.Architecture
  267. hashLabel := arch + "_sha256"
  268. versionInfo := config.VersionInfo
  269. jsonData, err := json.Marshal(versionInfo)
  270. if err != nil {
  271. return res, err
  272. }
  273. // Convert JSON to map
  274. var m map[string]interface{}
  275. err = json.Unmarshal(jsonData, &m)
  276. if err != nil {
  277. return res, err
  278. }
  279. containerData, ok := m[containerType].(map[string]interface{})
  280. if !ok {
  281. return nil, fmt.Errorf("%s data is not a map", containerType)
  282. }
  283. tag, ok := containerData["tag"].(string)
  284. if !ok {
  285. return nil, fmt.Errorf("'tag' is not a string")
  286. }
  287. hashValue, ok := containerData[hashLabel].(string)
  288. if !ok {
  289. return nil, fmt.Errorf("'%s' is not a string", hashLabel)
  290. }
  291. repo, ok := containerData["repo"].(string)
  292. if !ok {
  293. return nil, fmt.Errorf("'repo' is not a string")
  294. }
  295. res = make(map[string]string)
  296. res["tag"] = tag
  297. res["hash"] = hashValue
  298. res["repo"] = repo
  299. return res, nil
  300. }
  301. // stop a container with the name
  302. func StopContainerByName(containerName string) error {
  303. ctx := context.Background()
  304. cli, err := client.NewClientWithOpts(client.FromEnv)
  305. if err != nil {
  306. return err
  307. }
  308. // fetch all containers incl stopped
  309. containers, err := cli.ContainerList(ctx, types.ContainerListOptions{All: true})
  310. if err != nil {
  311. return err
  312. }
  313. for _, cont := range containers {
  314. for _, name := range cont.Names {
  315. if name == "/"+containerName {
  316. // Stop the container
  317. options := container.StopOptions{}
  318. if err := cli.ContainerStop(ctx, cont.ID, options); err != nil {
  319. return fmt.Errorf("failed to stop container %s: %v", containerName, err)
  320. }
  321. config.Logger.Info(fmt.Sprintf("Successfully stopped container %s\n", containerName))
  322. return nil
  323. }
  324. }
  325. }
  326. return fmt.Errorf("container with name %s not found", containerName)
  327. }
  328. // subscribe to docker events and feed them into eventbus
  329. func DockerListener() {
  330. ctx := context.Background()
  331. cli, err := client.NewClientWithOpts(client.FromEnv)
  332. if err != nil {
  333. config.Logger.Error(fmt.Sprintf("Error initializing Docker client: %v", err))
  334. return
  335. }
  336. messages, errs := cli.Events(ctx, types.EventsOptions{})
  337. for {
  338. select {
  339. case event := <-messages:
  340. // Convert the Docker event to our custom event and send it to the EventBus
  341. EventBus <- structs.Event{Type: event.Action, Data: event}
  342. case err := <-errs:
  343. config.Logger.Error(fmt.Sprintf("Docker event error: %v", err))
  344. }
  345. }
  346. }
  347. // periodically poll docker in case we miss something
  348. func DockerPoller() {
  349. ticker := time.NewTicker(10 * time.Second)
  350. for {
  351. select {
  352. case <-ticker.C:
  353. config.Logger.Info("polling docker")
  354. // todo (maybe not necessary?)
  355. // fetch the status of all containers and compare with app's state
  356. // if there's a change, send an event to the EventBus
  357. return
  358. }
  359. }
  360. }