Skip to content

Commit

Permalink
Merge pull request #77 from PierreBeucher/zod-schema
Browse files Browse the repository at this point in the history
Zod schema validation
  • Loading branch information
PierreBeucher authored Dec 19, 2024
2 parents a87c98d + 50d6071 commit 4c9dc3f
Show file tree
Hide file tree
Showing 58 changed files with 2,315 additions and 1,760 deletions.
4 changes: 0 additions & 4 deletions .mocharc.json

This file was deleted.

6 changes: 5 additions & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ tasks:
#

test-unit:
cmd: npx mocha ./test/unit/**/*.spec.ts
cmd: npx mocha --config test/unit/.mocharc.json ./test/unit --recursive

build-local:
cmd: docker build -t crafteo/cloudypad:local .
Expand All @@ -33,6 +33,10 @@ tasks:
test-create-and-destroy:
cmd: test/integ/create-and-destroy.sh

# Detect potential circular deps with Madge
test-circular-deps:
cmd: npx -y madge --ts-config ./tsconfig.json -c .

#
# Utils
Expand Down
11 changes: 10 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"smol-toml": "^1.3.0",
"sshpk": "^1.18.0",
"tslog": "^4.9.3",
"type-fest": "^4.22.1"
"type-fest": "^4.22.1",
"zod": "^3.24.1"
}
}
48 changes: 18 additions & 30 deletions src/configurators/ansible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,45 @@ import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import * as yaml from 'js-yaml';
import { StateManager } from '../core/state';
import { CommonProvisionConfigV1, CommonProvisionOutputV1 } from '../core/state/state';
import { InstanceConfigurator } from '../core/configurator';
import { getLogger, Logger } from '../log/utils';
import { AnsibleClient } from '../tools/ansible';

export interface AnsibleConfiguratorArgs {
instanceName: string
commonConfig: CommonProvisionConfigV1
commonOutput: CommonProvisionOutputV1
additionalAnsibleArgs?: string[]
}

export class AnsibleConfigurator implements InstanceConfigurator {

private readonly sm: StateManager
protected readonly logger: Logger
private readonly additionalAnsibleArgs: string[]
private readonly args: AnsibleConfiguratorArgs

constructor(sm: StateManager, additionalAnsibleArgs?: string[]){
this.sm = sm
this.logger = getLogger(sm.name())
this.additionalAnsibleArgs = additionalAnsibleArgs ?? []
constructor(args: AnsibleConfiguratorArgs){
this.args = args
this.logger = getLogger(args.instanceName)
}

async configure() {

const state = this.sm.get()
const ssh = this.args.commonConfig.ssh

this.logger.debug(`Running Ansible configuration`)

if(!state.ssh?.user || !state.ssh?.privateKeyPath) {
throw new Error(`Can't configure instance: SSH user or private key unknwon in state: ${JSON.stringify(state)}`)
}

if(!state.host) {
throw new Error(`Can't configure instance: hostname or public IP unknwon in state: ${JSON.stringify(state)}`)
}

const playbookPath = path.resolve(__dirname, "..", "..", "ansible", "playbook.yml"); // TODO more specific

this.logger.debug(`Using playbook ${playbookPath}`)

const inventoryContent = {
all: {
hosts: {
[state.name]: {
ansible_host: state.host,
ansible_user: state.ssh.user,
ansible_ssh_private_key_file: state.ssh.privateKeyPath
[this.args.instanceName]: {
ansible_host: this.args.commonOutput.host,
ansible_user: ssh.user,
ansible_ssh_private_key_file: ssh.privateKeyPath
},
},
},
Expand All @@ -59,16 +56,7 @@ export class AnsibleConfigurator implements InstanceConfigurator {

fs.writeFileSync(inventoryPath, yaml.dump(inventoryContent), 'utf8');

this.sm.update({
status: {
configuration: {
configured: true,
lastUpdate: Date.now()
}
}
})

const ansible = new AnsibleClient()
await ansible.runAnsible(inventoryPath, playbookPath, this.additionalAnsibleArgs)
await ansible.runAnsible(inventoryPath, playbookPath, this.args.additionalAnsibleArgs ?? [])
}
}
6 changes: 1 addition & 5 deletions src/core/const.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import * as path from 'path';

export const CLOUDYPAD_PROVIDER_AWS = "aws"
export const CLOUDYPAD_PROVIDER_PAPERSPACE = "paperspace"
export const CLOUDYPAD_PROVIDER_AZURE = "azure"
export const CLOUDYPAD_PROVIDER_GCP = "gcp"
export type CLOUDYPAD_PROVIDER = typeof CLOUDYPAD_PROVIDER_AWS | typeof CLOUDYPAD_PROVIDER_PAPERSPACE | typeof CLOUDYPAD_PROVIDER_AZURE | typeof CLOUDYPAD_PROVIDER_GCP

export const CLOUDYPAD_HOME = path.resolve(`${process.env.HOME || ''}/.cloudypad`)
export const CLOUDYPAD_INSTANCES_DIR = path.resolve(`${CLOUDYPAD_HOME}/instances`)
export const CLOUDYPAD_PROVIDER_LIST = [ CLOUDYPAD_PROVIDER_AWS, CLOUDYPAD_PROVIDER_PAPERSPACE, CLOUDYPAD_PROVIDER_AZURE, CLOUDYPAD_PROVIDER_GCP ] as const
Loading

0 comments on commit 4c9dc3f

Please sign in to comment.