@paralect/hive 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "deploy",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "author": "",
7
+ "license": "ISC",
8
+ "dependencies": {
9
+ "execa": "4.0.0",
10
+ "prompt-list": "3.2.0"
11
+ }
12
+ }
@@ -0,0 +1,48 @@
1
+ const path = require("path");
2
+ const rootDir = path.resolve(__dirname, "./../../../");
3
+
4
+ const ENV = process.env;
5
+
6
+ const config = {
7
+ rootDir,
8
+
9
+ environment: ENV.ENVIRONMENT || "staging",
10
+
11
+ namespace: ENV.NAMESPACE || "staging",
12
+
13
+ kubeConfig: ENV.KUBE_CONFIG,
14
+
15
+ home: ENV.HOME,
16
+
17
+ dockerRegistry: {
18
+ name: "paralect/hive-api",
19
+ username: ENV.DOCKER_AUTH_USERNAME,
20
+ password: ENV.DOCKER_AUTH_PASSWORD,
21
+
22
+ imageTag: ENV.IMAGE_TAG,
23
+ },
24
+ };
25
+
26
+ const deployConfig = {
27
+ dockerRepo: `${config.dockerRegistry.name}`,
28
+ dir: `${rootDir}`,
29
+ folder: "",
30
+ dockerFilePath: `${rootDir}/Dockerfile`,
31
+ dockerContextDir: rootDir
32
+ };
33
+
34
+ // Object.keys(deployConfig).forEach((serviceName) => {
35
+ // if (!deployConfig[serviceName].dockerFilePath) {
36
+ // deployConfig[
37
+ // serviceName
38
+ // ].dockerFilePath = `${deployConfig[serviceName].dir}/Dockerfile`;
39
+ // }
40
+
41
+ // if (!deployConfig[serviceName].dockerContextDir) {
42
+ // deployConfig[serviceName].dockerContextDir = deployConfig[serviceName].dir;
43
+ // }
44
+ // });
45
+
46
+ config.deploy = deployConfig;
47
+
48
+ module.exports = config;
@@ -0,0 +1,105 @@
1
+ const fs = require("fs");
2
+ const List = require("prompt-list");
3
+
4
+ const config = require("./config");
5
+ const { execCommand } = require("./util");
6
+
7
+
8
+ const buildAndPushImage = async ({
9
+ dockerFilePath,
10
+ dockerRepo,
11
+ dockerContextDir,
12
+ imageTag,
13
+ environment,
14
+ }) => {
15
+ await execCommand(`docker build \
16
+ --build-arg APP_ENV=${environment} \
17
+ -f ${dockerFilePath} \
18
+ -t ${dockerRepo} \
19
+ ${dockerContextDir}`);
20
+ await execCommand(`docker tag ${dockerRepo} ${imageTag}`);
21
+ await execCommand(`docker push ${imageTag}`);
22
+ await execCommand(`docker push ${dockerRepo}:latest`);
23
+ };
24
+
25
+ const pushToKubernetes = async ({ imageTag, appName, deployConfig }) => {
26
+ const deployDir = `${config.rootDir}/deploy/api`;
27
+
28
+ if (config.kubeConfig && !fs.existsSync(`${config.home}/.kube/config`)) {
29
+ console.log("Creating kubeconfig");
30
+ fs.mkdirSync(`${config.home}/.kube`);
31
+ fs.writeFileSync(`${config.home}/.kube/config`, config.kubeConfig);
32
+ }
33
+
34
+ let projectId = 'test';
35
+ let attachedDomains;
36
+
37
+ await execCommand(
38
+ `helm upgrade --force --install projects-${projectId} ${deployDir} \
39
+ --namespace apps --create-namespace \
40
+ --set containerRegistry=paralect/hive-api \
41
+ --set service=hive-api-${projectId} \
42
+ --set imagesVersion=${imageTag} \
43
+ --set domain={hive-api-${projectId}.paralect.co} \
44
+ --set projectId=${projectId} \
45
+ --set mongoDbUri='${process.env.MONGODB_URI}'
46
+ -f ${deployDir}/staging.yaml --timeout 35m`,
47
+ {
48
+ cwd: `/app`,
49
+ }
50
+ );
51
+ };
52
+
53
+ const deploy = async () => {
54
+ if (config.dockerRegistry.password) {
55
+ await execCommand(
56
+ `docker login --username ${config.dockerRegistry.username} --password ${config.dockerRegistry.password} registry.digitalocean.com`
57
+ );
58
+ }
59
+ const deployConfig = config.deploy;
60
+
61
+ let imageTag = config.dockerRegistry.imageTag;
62
+
63
+ if (!imageTag) {
64
+ const { stdout: branch } = await execCommand(
65
+ "git rev-parse --abbrev-ref HEAD",
66
+ { stdio: "pipe" }
67
+ );
68
+ const { stdout: commitSHA } = await execCommand("git rev-parse HEAD", {
69
+ stdio: "pipe",
70
+ });
71
+
72
+ imageTag = `${branch}.${commitSHA}`;
73
+ }
74
+
75
+ try {
76
+ await execCommand(
77
+ "kubectl delete secrets sh.helm.release.v1.apps-staging-api.v1"
78
+ );
79
+
80
+ await execCommand(
81
+ "kubectl delete secrets sh.helm.release.v1.apps-staging-api.v2"
82
+ );
83
+ } catch (err) {}
84
+
85
+ // push api image to registry
86
+ await buildAndPushImage({
87
+ ...deployConfig,
88
+ imageTag: `${deployConfig.dockerRepo}:${imageTag}`,
89
+ environment: config.environment,
90
+ });
91
+
92
+ // deploy api to kubernetes and deploy migrator through helm hooks
93
+ await pushToKubernetes({
94
+ imageTag,
95
+ appName: "api",
96
+ deployConfig,
97
+ });
98
+ };
99
+
100
+ deploy();
101
+
102
+ process.on("unhandledRejection", (error) => {
103
+ console.error(error);
104
+ process.exit(1);
105
+ });
@@ -0,0 +1,19 @@
1
+ const execa = require('execa');
2
+
3
+ const execCommand = async (command, options = {}) => {
4
+ let commandParts = command;
5
+
6
+ if (typeof command === 'string') {
7
+ commandParts = command.split(' ').filter(part => !!part.trim());
8
+ }
9
+
10
+ const commandName = commandParts.shift();
11
+ const commandArguments = commandParts;
12
+ console.log('command', command);
13
+
14
+ return execa(commandName, commandArguments, { stdio: 'inherit', ...options });
15
+ };
16
+
17
+ module.exports = {
18
+ execCommand,
19
+ };