/data/test/fsharp/2ab2aa96383b2234ba94eb697c227e6ddcf1f696Plugins.fs

https://github.com/aliostad/deep-learning-lang-detection · F# · 193 lines · 167 code · 22 blank · 4 comment · 10 complexity · 809432ef29b3a2611d87a42d1a81dffd MD5 · raw file

  1. namespace ClusterManagement
  2. type Plugin =
  3. | Ebs
  4. | S3fs
  5. member x.Name =
  6. match x with
  7. | Ebs -> "ebs"
  8. | S3fs -> "s3fs"
  9. type PluginSetting =
  10. { ClusterSettingName: string; PluginSettingName : string }
  11. type PluginOpt =
  12. { Option : string; Description : string }
  13. type PluginInfo =
  14. { Plugin : Plugin; ImageName : string; Tag : string; Settings : PluginSetting list; CreateOpts : PluginOpt list }
  15. module Plugins =
  16. let private nameMap, private imageMap, plugins =
  17. let (-->) s1 s2 = { ClusterSettingName = s1 ; PluginSettingName = s2 }
  18. let rawList =
  19. [ { Plugin = Ebs; ImageName = "rexray/ebs"; Tag = ""
  20. Settings =
  21. [ "AWS_ACCESS_KEY_ID" --> "EBS_ACCESSKEY"
  22. "AWS_ACCESS_KEY_SECRET" --> "EBS_SECRETKEY"
  23. "AWS_REGION" --> "EBS_REGION" ]
  24. CreateOpts = [ { Option = "size"; Description = "the size of the volume in gigabytes" } ] }
  25. { Plugin = S3fs; ImageName = "rexray/s3fs"; Tag = ""
  26. Settings =
  27. [ "AWS_ACCESS_KEY_ID" --> "S3FS_ACCESSKEY"
  28. "AWS_ACCESS_KEY_SECRET" --> "S3FS_SECRETKEY"
  29. "AWS_REGION" --> "S3FS_REGION" ]
  30. CreateOpts = [ { Option = "size"; Description = "the size of the volume in gigabytes" } ] } ]
  31. |> List.map (fun p -> { p with Tag = DockerImages.getImageTag p.ImageName })
  32. rawList |> Seq.map (fun p -> p.Plugin, p) |> Map.ofSeq,
  33. rawList |> Seq.map (fun p -> p.ImageName, p) |> Map.ofSeq,
  34. rawList |> List.map (fun p -> p.Plugin)
  35. let getPlugin p =
  36. match nameMap.TryFind p with
  37. | Some pl -> pl
  38. | None -> failwithf "Plugin '%s' was not found" p.Name
  39. let listPlugins wrapProcess=
  40. async {
  41. let! (result:DockerWrapper.DockerPlugin list) =
  42. DockerWrapper.listPlugins()
  43. |> wrapProcess
  44. |> Proc.startAndAwait
  45. return
  46. result
  47. |> List.choose (fun p -> imageMap.TryFind p.Image)
  48. }
  49. let installPlugin wrapProcess (plugin:Plugin) (c:ClusterConfig.MyClusterConfig) =
  50. async {
  51. if Env.isVerbose then printfn "installing and starting plugin '%s'." plugin.Name
  52. let forceConfig name =
  53. match ClusterConfig.getConfig name c with
  54. | Some va -> va
  55. | None -> failwithf "Expected config %s" name
  56. let pluginInfo = getPlugin plugin
  57. let settingsCmdLine =
  58. pluginInfo.Settings
  59. |> Seq.map (fun s -> sprintf "%s=%s" s.PluginSettingName (forceConfig s.ClusterSettingName))
  60. |> fun set -> System.String.Join(" ", set)
  61. let! (result : ProcessResults<unit>) =
  62. (sprintf "plugin inspect %s " pluginInfo.ImageName)
  63. |> Arguments.OfWindowsCommandLine
  64. |> DockerWrapper.createProcess
  65. |> wrapProcess
  66. |> Proc.startRaw
  67. if result.ExitCode = 0 then
  68. // exists -> disable and set settings, like EBS_ACCESSKEY=%s EBS_SECRETKEY=%s EBS_REGION=%s
  69. do!
  70. (sprintf "plugin disable --force %s" pluginInfo.ImageName)
  71. |> Arguments.OfWindowsCommandLine
  72. |> DockerWrapper.createProcess
  73. |> CreateProcess.ensureExitCode
  74. |> wrapProcess
  75. |> Proc.startAndAwait
  76. |> Async.Ignore
  77. do!
  78. (sprintf "plugin set %s %s"
  79. pluginInfo.ImageName settingsCmdLine)
  80. |> Arguments.OfWindowsCommandLine
  81. |> DockerWrapper.createProcess
  82. |> CreateProcess.ensureExitCode
  83. |> wrapProcess
  84. |> Proc.startAndAwait
  85. |> Async.Ignore
  86. else
  87. // Install docker plugin
  88. do!
  89. (sprintf "plugin install --disable --grant-all-permissions %s %s"
  90. pluginInfo.ImageName settingsCmdLine)
  91. |> Arguments.OfWindowsCommandLine
  92. |> DockerWrapper.createProcess
  93. |> CreateProcess.ensureExitCode
  94. |> wrapProcess
  95. |> Proc.startAndAwait
  96. |> Async.Ignore
  97. // sudo docker plugin upgrade --skip-remote-check --grant-all-permissions rexray/ebs:0.9.0 rexray/ebs:0.8.2
  98. //
  99. do!
  100. (sprintf "plugin upgrade --skip-remote-check --grant-all-permissions %s %s:%s"
  101. pluginInfo.ImageName pluginInfo.ImageName pluginInfo.Tag)
  102. |> Arguments.OfWindowsCommandLine
  103. |> DockerWrapper.createProcess
  104. |> CreateProcess.ensureExitCode
  105. |> wrapProcess
  106. |> Proc.startAndAwait
  107. |> Async.Ignore
  108. do!
  109. (sprintf "plugin enable %s" pluginInfo.ImageName)
  110. |> Arguments.OfWindowsCommandLine
  111. |> DockerWrapper.createProcess
  112. |> CreateProcess.ensureExitCode
  113. |> wrapProcess
  114. |> Proc.startAndAwait
  115. |> Async.Ignore
  116. }
  117. let uninstallPlugin wrapProcess (plugin:Plugin) =
  118. async {
  119. if Env.isVerbose then printfn "removing plugin '%s'." plugin.Name
  120. let pluginInfo = getPlugin plugin
  121. let! (result : ProcessResults<unit>) =
  122. (sprintf "plugin inspect %s" pluginInfo.ImageName)
  123. |> Arguments.OfWindowsCommandLine
  124. |> DockerWrapper.createProcess
  125. |> wrapProcess
  126. |> Proc.startRaw
  127. if result.ExitCode = 0 then
  128. do!
  129. (sprintf "plugin disable --force %s" pluginInfo.ImageName)
  130. |> Arguments.OfWindowsCommandLine
  131. |> DockerWrapper.createProcess
  132. |> CreateProcess.ensureExitCode
  133. |> wrapProcess
  134. |> Proc.startAndAwait
  135. |> Async.Ignore
  136. do!
  137. (sprintf "plugin rm --force %s"
  138. pluginInfo.ImageName)
  139. |> Arguments.OfWindowsCommandLine
  140. |> DockerWrapper.createProcess
  141. |> CreateProcess.ensureExitCode
  142. |> wrapProcess
  143. |> Proc.startAndAwait
  144. |> Async.Ignore
  145. }
  146. let listClusterPlugins cluster =
  147. listPlugins (DockerMachine.runSudoDockerOnNode cluster "master-01")
  148. let installToCluster cluster plugin =
  149. async {
  150. let i = Deploy.getInfoInternal cluster [||]
  151. for node in i.Nodes do
  152. do! installPlugin
  153. (DockerMachine.runSudoDockerOnNode cluster node.Name)
  154. plugin
  155. i.ClusterConfig
  156. }
  157. let uninstallFromCluster cluster plugin =
  158. async {
  159. let i = Deploy.getInfoInternal cluster [||]
  160. for node in i.Nodes do
  161. do! uninstallPlugin
  162. (DockerMachine.runSudoDockerOnNode cluster node.Name)
  163. plugin
  164. }
  165. let ensurePluginInstalled cluster plugin =
  166. async {
  167. let! plugins = listPlugins (DockerMachine.runSudoDockerOnNode cluster "master-01")
  168. plugins
  169. |> Seq.exists (fun p -> p.Plugin = plugin)
  170. |> fun b -> if not b then failwithf "Plugin '%s' needs to be installed on cluster '%s' first!" plugin.Name cluster
  171. }