index.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. import * as awsx from '@pulumi/awsx'
  2. import * as aws from '@pulumi/aws'
  3. import * as eks from '@pulumi/eks'
  4. import * as k8s from '@pulumi/kubernetes'
  5. import * as pulumi from '@pulumi/pulumi'
  6. import * as fs from 'fs'
  7. const dns = require('dns')
  8. const awsConfig = new pulumi.Config('aws')
  9. const config = new pulumi.Config()
  10. const wsProviderEndpointURI = config.require('wsProviderEndpointURI')
  11. const isAnonymous = config.require('isAnonymous') === 'true'
  12. const lbReady = config.get('isLoadBalancerReady') === 'true'
  13. const name = 'storage-node'
  14. const colossusPort = parseInt(config.get('colossusPort') || '3000')
  15. const storage = parseInt(config.get('storage') || '40')
  16. let additionalParams: string[] | pulumi.Input<string>[] = []
  17. let volumeMounts: pulumi.Input<pulumi.Input<k8s.types.input.core.v1.VolumeMount>[]> = []
  18. let caddyVolumeMounts: pulumi.Input<pulumi.Input<k8s.types.input.core.v1.VolumeMount>[]> = []
  19. let volumes: pulumi.Input<pulumi.Input<k8s.types.input.core.v1.Volume>[]> = []
  20. // Create a VPC for our cluster.
  21. const vpc = new awsx.ec2.Vpc('vpc', { numberOfAvailabilityZones: 2 })
  22. // Create an EKS cluster with the default configuration.
  23. const cluster = new eks.Cluster('eksctl-my-cluster', {
  24. vpcId: vpc.id,
  25. subnetIds: vpc.publicSubnetIds,
  26. instanceType: 't2.micro',
  27. providerCredentialOpts: {
  28. profileName: awsConfig.get('profile'),
  29. },
  30. })
  31. // Export the cluster's kubeconfig.
  32. export const kubeconfig = cluster.kubeconfig
  33. // Create a repository
  34. const repo = new awsx.ecr.Repository('colossus-image')
  35. // Build an image and publish it to our ECR repository.
  36. export const colossusImage = repo.buildAndPushImage({
  37. dockerfile: '../../../colossus.Dockerfile',
  38. context: '../../../',
  39. })
  40. // Create a Kubernetes Namespace
  41. const ns = new k8s.core.v1.Namespace(name, {}, { provider: cluster.provider })
  42. // Export the Namespace name
  43. export const namespaceName = ns.metadata.name
  44. const appLabels = { appClass: name }
  45. const pvc = new k8s.core.v1.PersistentVolumeClaim(
  46. `${name}-pvc`,
  47. {
  48. metadata: {
  49. labels: appLabels,
  50. namespace: namespaceName,
  51. name: `${name}-pvc`,
  52. },
  53. spec: {
  54. accessModes: ['ReadWriteOnce'],
  55. resources: {
  56. requests: {
  57. storage: `${storage}Gi`,
  58. },
  59. },
  60. },
  61. },
  62. { provider: cluster.provider }
  63. )
  64. volumes.push({
  65. name: 'ipfs-data',
  66. persistentVolumeClaim: {
  67. claimName: `${name}-pvc`,
  68. },
  69. })
  70. // Create a LoadBalancer Service for the Deployment
  71. const service = new k8s.core.v1.Service(
  72. name,
  73. {
  74. metadata: {
  75. labels: appLabels,
  76. namespace: namespaceName,
  77. },
  78. spec: {
  79. type: 'LoadBalancer',
  80. ports: [
  81. { name: 'http', port: 80 },
  82. { name: 'https', port: 443 },
  83. ],
  84. selector: appLabels,
  85. },
  86. },
  87. {
  88. provider: cluster.provider,
  89. }
  90. )
  91. // Export the Service name and public LoadBalancer Endpoint
  92. export const serviceName = service.metadata.name
  93. // When "done", this will print the hostname
  94. export let serviceHostname: pulumi.Output<string>
  95. serviceHostname = service.status.loadBalancer.ingress[0].hostname
  96. export let appLink: pulumi.Output<string>
  97. if (lbReady) {
  98. async function lookupPromise(url: string) {
  99. return new Promise((resolve, reject) => {
  100. dns.lookup(url, (err: any, address: any) => {
  101. if (err) reject(err)
  102. resolve(address)
  103. })
  104. })
  105. }
  106. const lbIp = serviceHostname.apply((dnsName) => {
  107. return lookupPromise(dnsName)
  108. })
  109. const caddyConfig = pulumi.interpolate`${lbIp}.nip.io {
  110. reverse_proxy localhost:${colossusPort}
  111. }`
  112. const keyConfig = new k8s.core.v1.ConfigMap(name, {
  113. metadata: { namespace: namespaceName, labels: appLabels },
  114. data: { 'fileData': caddyConfig },
  115. })
  116. const keyConfigName = keyConfig.metadata.apply((m) => m.name)
  117. caddyVolumeMounts.push({
  118. mountPath: '/etc/caddy/Caddyfile',
  119. name: 'caddy-volume',
  120. subPath: 'fileData',
  121. })
  122. volumes.push({
  123. name: 'caddy-volume',
  124. configMap: {
  125. name: keyConfigName,
  126. },
  127. })
  128. appLink = pulumi.interpolate`https://${lbIp}.nip.io`
  129. lbIp.apply((value) => console.log(`You can now access the app at: ${value}.nip.io`))
  130. if (!isAnonymous) {
  131. const remoteKeyFilePath = '/joystream/key-file.json'
  132. const providerId = config.require('providerId')
  133. const keyFile = config.require('keyFile')
  134. const publicUrl = config.get('publicURL') ? config.get('publicURL')! : appLink
  135. const keyConfig = new k8s.core.v1.ConfigMap('key-config', {
  136. metadata: { namespace: namespaceName, labels: appLabels },
  137. data: { 'fileData': fs.readFileSync(keyFile).toString() },
  138. })
  139. const keyConfigName = keyConfig.metadata.apply((m) => m.name)
  140. additionalParams = ['--provider-id', providerId, '--key-file', remoteKeyFilePath, '--public-url', publicUrl]
  141. volumeMounts.push({
  142. mountPath: remoteKeyFilePath,
  143. name: 'keyfile-volume',
  144. subPath: 'fileData',
  145. })
  146. volumes.push({
  147. name: 'keyfile-volume',
  148. configMap: {
  149. name: keyConfigName,
  150. },
  151. })
  152. const passphrase = config.get('passphrase')
  153. if (passphrase) {
  154. additionalParams.push('--passphrase', passphrase)
  155. }
  156. }
  157. }
  158. if (isAnonymous) {
  159. additionalParams.push('--anonymous')
  160. }
  161. // Create a Deployment
  162. const deployment = new k8s.apps.v1.Deployment(
  163. name,
  164. {
  165. metadata: {
  166. namespace: namespaceName,
  167. labels: appLabels,
  168. },
  169. spec: {
  170. replicas: 1,
  171. selector: { matchLabels: appLabels },
  172. template: {
  173. metadata: {
  174. labels: appLabels,
  175. },
  176. spec: {
  177. hostname: 'ipfs',
  178. containers: [
  179. {
  180. name: 'ipfs',
  181. image: 'ipfs/go-ipfs:latest',
  182. ports: [{ containerPort: 5001 }, { containerPort: 8080 }],
  183. command: ['/bin/sh', '-c'],
  184. args: [
  185. 'set -e; \
  186. /usr/local/bin/start_ipfs config profile apply lowpower; \
  187. /usr/local/bin/start_ipfs config --json Gateway.PublicGateways \'{"localhost": null }\'; \
  188. /usr/local/bin/start_ipfs config Datastore.StorageMax 200GB; \
  189. /sbin/tini -- /usr/local/bin/start_ipfs daemon --migrate=true',
  190. ],
  191. volumeMounts: [
  192. {
  193. name: 'ipfs-data',
  194. mountPath: '/data/ipfs',
  195. },
  196. ],
  197. },
  198. // {
  199. // name: 'httpd',
  200. // image: 'crccheck/hello-world',
  201. // ports: [{ name: 'hello-world', containerPort: 8000 }],
  202. // },
  203. {
  204. name: 'caddy',
  205. image: 'caddy',
  206. ports: [
  207. { name: 'caddy-http', containerPort: 80 },
  208. { name: 'caddy-https', containerPort: 443 },
  209. ],
  210. volumeMounts: caddyVolumeMounts,
  211. },
  212. {
  213. name: 'colossus',
  214. image: colossusImage,
  215. env: [
  216. {
  217. name: 'WS_PROVIDER_ENDPOINT_URI',
  218. // example 'wss://18.209.241.63.nip.io/'
  219. value: wsProviderEndpointURI,
  220. },
  221. {
  222. name: 'DEBUG',
  223. value: 'joystream:*',
  224. },
  225. ],
  226. volumeMounts,
  227. command: [
  228. 'yarn',
  229. 'colossus',
  230. '--ws-provider',
  231. wsProviderEndpointURI,
  232. '--ipfs-host',
  233. 'ipfs',
  234. ...additionalParams,
  235. ],
  236. ports: [{ containerPort: colossusPort }],
  237. },
  238. ],
  239. volumes,
  240. },
  241. },
  242. },
  243. },
  244. {
  245. provider: cluster.provider,
  246. }
  247. )
  248. // Export the Deployment name
  249. export const deploymentName = deployment.metadata.name