From afb35ebad8561b29dbcbef6d4a5d13726a067872 Mon Sep 17 00:00:00 2001 From: garrettmills Date: Sun, 14 Jun 2020 09:07:26 -0500 Subject: [PATCH] fix clone state; example routine --- app/classes/state/git/CloneState.js | 2 + app/services/hosts.service.js | 3 +- app/services/routines.service.js | 9 +- config/routines/example.config.js | 147 ++++++++++++++++++++++++++++ config/routines/login.config.js | 20 ---- config/routines/tmpdir.config.js | 18 ---- config/routines/updates.config.js | 16 --- package.json | 2 +- yarn.lock | 8 +- 9 files changed, 162 insertions(+), 63 deletions(-) create mode 100644 config/routines/example.config.js delete mode 100644 config/routines/login.config.js delete mode 100644 config/routines/tmpdir.config.js delete mode 100644 config/routines/updates.config.js diff --git a/app/classes/state/git/CloneState.js b/app/classes/state/git/CloneState.js index f0d4bb0..f0f75bf 100644 --- a/app/classes/state/git/CloneState.js +++ b/app/classes/state/git/CloneState.js @@ -1,5 +1,7 @@ const AbstractGitState = require('./AbstractGitState') +// TODO add 'bare' option +// TODO add 'new_repo' option (rm -rf .git && git init) class CloneState extends AbstractGitState { async apply() { diff --git a/app/services/hosts.service.js b/app/services/hosts.service.js index 9b8b48e..ebc0341 100644 --- a/app/services/hosts.service.js +++ b/app/services/hosts.service.js @@ -15,11 +15,12 @@ class hosts extends Service { get(name) { const config = this.config[name] - config.name = name if ( !config ) { throw new Error(`Could not get host ${name}: No such host configured.`) } + config.name = name + if ( config.type === 'localhost' ) { const host = this.app.di().make(LocalHost, config) this._running_hosts.push(host) diff --git a/app/services/routines.service.js b/app/services/routines.service.js index 7e4b398..a195148 100644 --- a/app/services/routines.service.js +++ b/app/services/routines.service.js @@ -12,18 +12,21 @@ const Routine = require('../classes/routine/Routine') */ class RoutinesService extends Service { static get services() { - return [...super.services, 'app', 'configs', 'hosts'] + return [...super.services, 'injector', 'configs', 'hosts'] } async get(name) { - this.app.make(Routine) const config = this.configs.get(`routines:${name}`) + if ( !config ) { + throw new Error(`Unable to find a routine with the name: ${name}`) + } + const hosts = {} for ( const host_name of config.hosts ) { hosts[host_name] = await this.hosts.get(host_name) } - return new Routine(hosts, config, config.type) + return this.injector.make(Routine, hosts, config, config.type) } } diff --git a/config/routines/example.config.js b/config/routines/example.config.js new file mode 100644 index 0000000..267488f --- /dev/null +++ b/config/routines/example.config.js @@ -0,0 +1,147 @@ +const example_routine = { + type: 'checks', + hosts: ['localhost', 'other_server'], + steps: [ + { + // State: the file exists on the host + type: 'fs.file', + host: 'localhost', // localhost | other_server + path: '/tmp/some.file', + }, + { + // State: the directory exists on the host + type: 'fs.directory', + host: 'localhost', + path: '/tmp/some.directory', + }, + { + // State: the archive is unpacked into the destination + type: 'fs.unpack', + host: 'localhost', + path: '/tmp/some.zip', // or /tmp/some.tar.gz + destination: '/tmp/destination.dir', + format: 'zip', // Optional: tar | zip (by default, from file extension) + }, + { + // State: the path is packed into a destination archive + type: 'fs.pack', + host: 'localhost', + path: '/tmp/some.directory/or.file', + destination: '/tmp/packed.tar.gz', + format: 'tar', // Optional: tar | zip (by default, from file extension) + }, + { + // State: the fs permissions are set to the specified level + type: 'fs.permission', + host: 'localhost', + path: '/tmp/some.directory/or.file', + level: '644', // Some chmod permission level + recursive: true, // default: false, + revert_to: '555', // Permissions to revert to on undo, optional + }, + { + // State: the fs owners are set to the specified user/group + type: 'fs.ownership', + host: 'localhost', + owners: { // Specify one or both of the following: + user: 'some_user', + group: 'some_group', + }, + path: '/tmp/some.file/or.dir', + recursive: true, // default: false + revert_to: { + user: 'root', + group: 'root', + }, + }, + { + // State: the source repo is cloned into the path + type: 'git.clone', + host: 'localhost', + path: '/tmp/some.dir.on.host', + source: 'https://git.host/some/repo', + }, + { + // State: the specified ref is checked out + type: 'git.checkout', + host: 'localhost', + path: '/tmp/some.dir.on.host', + target: 'develop', // ref to check out + revert_to: 'master', // ref to revert to on undo + }, + { + // State: the specified tag exists + type: 'git.tag', + host: 'localhost', + path: '/tmp/some.dir.on.host', + tag: 'rc-1', // tag to be created + }, + { + // State: the command has been run + type: 'os.cmd', + host: 'localhost', + cmd: 'useradd some_user', // Command to run on execute + reverse: 'userdel some_user', // Command to run on undo + }, + { + // State: the host is alive and responding + type: 'os.alive', + host: 'localhost', + }, + { + // State: the package is installed on the host + type: 'package.present', + host: 'localhost', + package: 'gcc', + }, + { + // State: the package is not installed on the host + type: 'package.absent', + host: 'localhost', + package: 'gcc', + }, + { + // State: the packages are all up to date + type: 'package.updates', + host: 'localhost', + }, + { + // State: the package cache has been cleared + type: 'package.cache.clear', + host: 'localhost', + }, + { + // State: the specified service(s) is (are) running + type: 'service.running', + host: 'localhost', + service: 'apache2', // or array of services: ['apache2', 'redis'] + }, + { + // State: the specified service(s) is (are) stopped + type: 'service.stopped', + host: 'localhost', + service: 'apache2', // or array of services: ['apache2', 'redis'] + }, + { + // State: the specified service(s) has (have) been restarted + type: 'service.restarted', + host: 'localhost', + service: 'apache2', // or array of services: ['apache2', 'redis'] + }, + { + // State: the service daemon has been reloaded + type: 'service.daemon.reloaded', + host: 'localhost', + }, + { + // State: the source file has been downloaded to the destination path + type: 'web.download', + host: 'localhost', + method: 'get', // HTTP verb + source: 'https://static.garrettmills.dev/assets/flitter/flitter_f.png', + path: '/tmp/flitter_f.png', + }, + ], +} + +module.exports = exports = example_routine diff --git a/config/routines/login.config.js b/config/routines/login.config.js deleted file mode 100644 index 44af28b..0000000 --- a/config/routines/login.config.js +++ /dev/null @@ -1,20 +0,0 @@ -const login_config = { - type: 'checks', - hosts: ['core', 'localhost', 'edge'], - steps: [ - { - type: 'os.alive', - host: 'core', - }, - { - type: 'os.alive', - host: 'edge', - }, - { - type: 'os.alive', - host: 'localhost', - }, - ], -} - -module.exports = exports = login_config diff --git a/config/routines/tmpdir.config.js b/config/routines/tmpdir.config.js deleted file mode 100644 index 9e59c32..0000000 --- a/config/routines/tmpdir.config.js +++ /dev/null @@ -1,18 +0,0 @@ -const tmpdir_config = { - type: 'checks', - hosts: ['core', 'localhost'], - steps: [ - { - type: 'fs.directory', - host: 'core', - path: '/tmp/glmdev', - }, - { - type: 'fs.directory', - host: 'localhost', - path: '/tmp/glmdev', - }, - ], -} - -module.exports = exports = tmpdir_config diff --git a/config/routines/updates.config.js b/config/routines/updates.config.js deleted file mode 100644 index df3f809..0000000 --- a/config/routines/updates.config.js +++ /dev/null @@ -1,16 +0,0 @@ -const updates_config = { - type: 'checks', - hosts: ['core', 'localhost'], - steps: [ - { - type: 'package.updates', - host: 'core', - }, - { - type: 'package.updates', - host: 'localhost', - }, - ], -} - -module.exports = exports = updates_config diff --git a/package.json b/package.json index d5fea93..87c6532 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "flitter-cli": "^0.16.0", "flitter-di": "^0.5.0", "flitter-flap": "^0.5.2", - "libflitter": "^0.53.1", + "libflitter": "^0.54.0", "moment": "^2.24.0", "scp": "^0.0.3", "ssh2": "^0.8.7", diff --git a/yarn.lock b/yarn.lock index 895b80c..3537842 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2211,10 +2211,10 @@ leven@^1.0.2: resolved "https://registry.yarnpkg.com/leven/-/leven-1.0.2.tgz#9144b6eebca5f1d0680169f1a6770dcea60b75c3" integrity sha1-kUS27ryl8dBoAWnxpncNzqYLdcM= -libflitter@^0.53.1: - version "0.53.1" - resolved "https://registry.yarnpkg.com/libflitter/-/libflitter-0.53.1.tgz#30b1838763a228fba8b9c820d2cad501c3aa0117" - integrity sha512-EK3okZyt0pmnpsZNx2lYOIcwgtmSOEPh4a5xE3pXM9RVc3dtXXscgJ5h9OvLTIN9WfRc7T5VTdpOjeAK6Xmysg== +libflitter@^0.54.0: + version "0.54.0" + resolved "https://registry.yarnpkg.com/libflitter/-/libflitter-0.54.0.tgz#5758fd0069de0ed75a80385c66809078b8210c36" + integrity sha512-/pRgUD5dXdpEPxR6B3Do5BZQTGLrTYj3u0ZxYr7GViB4NysLePkR8Vg3gBzuNYUkEznzWxDR8VcmtuIiEowhFw== dependencies: colors "^1.3.3" connect-mongodb-session "^2.2.0"