presidium 0.16.28 → 0.18.0

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.
package/DockerService.js CHANGED
@@ -107,35 +107,39 @@ const DockerService = function (options) {
107
107
  if (this == null || this.constructor != DockerService) {
108
108
  return new DockerService(options)
109
109
  }
110
+ this.serviceOptions = pick(dockerServiceOptions)(options)
110
111
  this.name = options.name
111
112
  this.docker = new Docker()
112
113
  this.version = null
113
114
  this.spec = null
114
115
  this.replicas = options.replicas
115
-
116
- this.ready = this.docker.inspectService(this.name).then(pipe([
117
- switchCase([
118
- eq(404, get('status')),
119
- pipe([
120
- () => this.docker.createService(
121
- this.name,
122
- pick(dockerServiceOptions)(options)),
123
- tap(async response => {
124
- if (!response.ok) {
125
- throw new Error(await response.text())
126
- }
127
- }),
128
- ]),
129
- pipe([
130
- tap(() => this.synchronize()),
131
- () => this.update(pick(dockerServiceOptions)(options)),
132
- ]),
133
- ]),
134
- tap(() => this.synchronize()),
135
- ]))
136
116
  return this
137
117
  }
138
118
 
119
+ /**
120
+ * @name DockerService.prototype.deploy
121
+ *
122
+ * @synopsis
123
+ * ```coffeescript [specscript]
124
+ * DockerService(...).deploy()
125
+ * ```
126
+ */
127
+ DockerService.prototype.deploy = async function deploy() {
128
+ const { status } = await this.docker.inspectService(this.name)
129
+
130
+ if (status == 404) {
131
+ const { name, serviceOptions } = this
132
+ const response = await this.docker.createService(name, serviceOptions)
133
+ if (!response.ok) {
134
+ throw new Error(await response.text())
135
+ }
136
+ } else {
137
+ await this.synchronize()
138
+ await this.update(this.serviceOptions)
139
+ }
140
+ await this.synchronize()
141
+ }
142
+
139
143
  // new DockerService().synchronize() -> Promise<>
140
144
  DockerService.prototype.synchronize = function dockerServiceSynchronize() {
141
145
  return this.docker.inspectService(this.name).then(pipe([
@@ -21,6 +21,8 @@ const test = new Test('DockerService', DockerService)
21
21
  image: 'nginx:1.19',
22
22
  replicas: 1,
23
23
  }, async function (myService) {
24
+ await myService.deploy()
25
+
24
26
  {
25
27
  const info = await myService.inspect()
26
28
  this.serviceId = info.ID
@@ -106,7 +108,7 @@ const test = new Test('DockerService', DockerService)
106
108
  image: 'nginx:1.19',
107
109
  replicas: 2,
108
110
  }, async function (myService) {
109
- await myService.ready
111
+ await myService.deploy()
110
112
  // TODO test for update on construction assert.deepEqual(myService.spec, this.myServiceSpec)
111
113
  })
112
114
 
@@ -116,8 +118,8 @@ const test = new Test('DockerService', DockerService)
116
118
  replicas: 2,
117
119
  restart: 'always', // coulda fooled me
118
120
  }, async function (errorService) {
119
- assert.rejects(
120
- always(errorService.ready),
121
+ await assert.rejects(
122
+ errorService.deploy(),
121
123
  new Error('{"message":"invalid RestartCondition: \\"always\\""}\n'))
122
124
  })
123
125
 
package/DockerSwarm.js CHANGED
@@ -1,81 +1,63 @@
1
- const assert = require('assert')
2
- const rubico = require('rubico')
1
+ require('rubico/global')
3
2
  const Docker = require('./Docker')
4
3
 
5
- const {
6
- pipe, tap,
7
- switchCase, tryCatch,
8
- fork, assign, get, pick, omit,
9
- map, filter, reduce, transform, flatMap,
10
- and, or, not, any, all,
11
- eq, gt, lt, gte, lte,
12
- thunkify, always,
13
- curry, __,
14
- } = rubico
15
-
16
4
  /**
17
5
  * @name DockerSwarm
18
6
  *
19
7
  * @synopsis
20
8
  * ```coffeescript [specscript]
21
- * new DockerSwarm(
22
- * advertiseAddr string, // url advertised to other nodes, e.g. 'eth0:2377'
23
- * options {
24
- * joinToken: string, // worker or manager join token
25
- * remoteAddrs: Array<string>, // urls of manager nodes already participating in the swarm
26
- * },
27
- * ) -> DockerSwarm
9
+ * new DockerSwarm(advertiseAddr string) -> DockerSwarm
28
10
  * ```
11
+ *
12
+ * @description
13
+ * DockerSwarm interface
14
+ *
15
+ * ## Resources
16
+ * https://boxboat.com/2016/08/17/whats-docker-swarm-advertise-addr/
29
17
  */
30
- const DockerSwarm = function (advertiseAddr, options = {}) {
31
- if (this == null || this.constructor != DockerSwarm) {
32
- return new DockerSwarm(advertiseAddr, options)
33
- }
18
+ const DockerSwarm = function (advertiseAddr) {
19
+ this.advertiseAddr = advertiseAddr
34
20
  this.docker = new Docker()
35
- this.ready = this.docker.inspectSwarm().then(switchCase([
36
- or([
37
- eq(404, get('status')),
38
- eq(503, get('status')),
39
- ]),
40
- options.joinToken == null ? async () => {
41
- await this.docker.initSwarm(advertiseAddr)
42
- await this.synchronize()
43
- } : async () => {
44
- const response = await this.docker.joinSwarm(advertiseAddr, options.joinToken, {
45
- remoteAddrs: options.remoteAddrs,
46
- })
47
- if (!response.ok) {
48
- throw new Error(await response.text())
49
- }
50
- },
51
- async response => {
52
- const data = await response.json()
53
- this.version = data.Version.Index
54
- this.spec = data.Spec
55
- this.swarmData = data
56
- },
57
- ]))
58
- this.version = null
59
- this.spec = null
60
- this.swarmData = null
61
21
  return this
62
22
  }
63
23
 
64
- // new DockerSwarm().synchronize() -> Promise<>
65
- DockerSwarm.prototype.synchronize = function dockerServiceSynchronize() {
66
- return this.docker.inspectSwarm().then(pipe([
67
- tap(async response => {
68
- if (!response.ok) {
69
- throw new Error(await response.text())
70
- }
71
- }),
72
- response => response.json(),
73
- data => {
74
- this.version = data.Version.Index
75
- this.spec = data.Spec
76
- this.swarmData = data
77
- },
78
- ]))
24
+ /**
25
+ * @name DockerSwarm.prototype.init
26
+ *
27
+ * @synopsis
28
+ * ```coffeescript [specscript]
29
+ * new DockerSwarm(...).init() -> Promise<>
30
+ * ```
31
+ */
32
+ DockerSwarm.prototype.init = async function init() {
33
+ await this.docker.initSwarm(this.advertiseAddr).then(async response => {
34
+ if (!response.ok) {
35
+ throw new Error(await response.text())
36
+ }
37
+ })
38
+ }
39
+
40
+ /**
41
+ * @name DockerSwarm.prototype.join
42
+ *
43
+ * @synopsis
44
+ * ```coffeescript [specscript]
45
+ * new DockerSwarm(...).join(options {
46
+ * joinToken: string, // worker or manager join token
47
+ * remoteAddrs: Array<string>, // urls of manager nodes already participating in the swarm
48
+ * }) -> Promise<>
49
+ * ```
50
+ */
51
+
52
+ DockerSwarm.prototype.join = async function join(options) {
53
+ const { joinToken, remoteAddrs } = options
54
+ await this.docker.joinSwarm(this.advertiseAddr, joinToken, {
55
+ remoteAddrs,
56
+ }).then(async response => {
57
+ if (!response.ok) {
58
+ throw new Error(await response.text())
59
+ }
60
+ })
79
61
  }
80
62
 
81
63
  // DockerSwarm(address).inspect() -> Promise<Object>
@@ -113,19 +95,15 @@ DockerSwarm.prototype.leave = function dockerSwarmLeave(options) {
113
95
  * @description
114
96
  * https://docs.docker.com/engine/api/v1.40/#operation/SwarmUpdate
115
97
  * https://docs.docker.com/engine/reference/commandline/swarm_update/
116
- */
98
+ *
99
+ * @deprecated
117
100
  DockerSwarm.prototype.update = async function dockerSwarmUpdate(options) {
118
101
  await this.ready
119
102
  return this.docker.updateSwarm({
120
103
  version: this.version,
121
104
  spec: this.spec,
122
105
  ...options,
123
- }).then(pipe.sync([
124
- tap.sync(() => {
125
- this.ready = this.synchronize()
126
- }),
127
- always(this.spec),
128
- ]))
129
- }
106
+ })
107
+ } */
130
108
 
131
109
  module.exports = DockerSwarm
@@ -4,59 +4,27 @@ const Docker = require('./Docker')
4
4
  const DockerSwarm = require('./DockerSwarm')
5
5
  const inspect = require('util').inspect
6
6
 
7
- module.exports = Test('DockerSwarm', DockerSwarm)
8
- .before(async function () {
9
- this.docker = new Docker()
10
- await this.docker.leaveSwarm({ force: true })
11
- })
12
- .case('[::1]:2377', async function (localSwarm) {
13
- assert(localSwarm.spec == null)
14
- assert(localSwarm.version == null)
15
- await localSwarm.ready
16
- assert(localSwarm.spec != null)
17
- assert(localSwarm.version != null)
7
+ const test = Test('DockerSwarm', async function () {
8
+ this.docker = new Docker()
9
+ await this.docker.leaveSwarm({ force: true })
18
10
 
19
- {
20
- const prevTokens = (await localSwarm.inspect()).JoinTokens
21
- const result = await localSwarm.update({
22
- rotateWorkerToken: true,
23
- rotateManagerToken: true,
24
- rotateManagerUnlockKey: true,
25
- autolock: true,
26
- })
11
+ const advertiseAddr = '[::1]:2377'
12
+ const localSwarm = new DockerSwarm(advertiseAddr)
27
13
 
28
- assert.strictEqual(localSwarm.spec.EncryptionConfig.AutoLockManagers, false)
29
- await localSwarm.ready
30
- assert.strictEqual(localSwarm.spec.EncryptionConfig.AutoLockManagers, true)
31
- const newTokens = (await localSwarm.inspect()).JoinTokens
32
- assert(prevTokens.Worker != newTokens.Worker)
33
- assert(prevTokens.Manager != newTokens.Manager)
34
- }
14
+ console.log('initializing swarm')
15
+ await localSwarm.init()
35
16
 
36
- {
37
- const result = await localSwarm.update({
38
- snapshotInterval: 20000,
39
- keepOldSnapshots: 3,
40
- logEntriesForSlowFollowers: 1000,
41
- electionTick: 20,
42
- heartbeatTick: 2,
43
- })
17
+ await localSwarm.inspect().then(console.log)
44
18
 
45
- assert.strictEqual(localSwarm.spec.Raft.SnapshotInterval, 10000)
46
- assert.strictEqual(localSwarm.spec.Raft.KeepOldSnapshots, 0)
47
- assert.strictEqual(localSwarm.spec.Raft.LogEntriesForSlowFollowers, 500)
48
- assert.strictEqual(localSwarm.spec.Raft.ElectionTick, 10)
49
- assert.strictEqual(localSwarm.spec.Raft.HeartbeatTick, 1)
50
- await localSwarm.ready
51
- assert.strictEqual(localSwarm.spec.Raft.SnapshotInterval, 20000)
52
- assert.strictEqual(localSwarm.spec.Raft.KeepOldSnapshots, 3)
53
- assert.strictEqual(localSwarm.spec.Raft.LogEntriesForSlowFollowers, 1000)
54
- assert.strictEqual(localSwarm.spec.Raft.ElectionTick, 20)
55
- assert.strictEqual(localSwarm.spec.Raft.HeartbeatTick, 2)
56
- }
19
+ {
20
+ console.log('leaving swarm')
21
+ const result = await localSwarm.leave({ force: true })
22
+ assert.deepEqual(result, { message: '' })
23
+ }
24
+ }).case()
57
25
 
58
- {
59
- const result = await localSwarm.leave({ force: true })
60
- assert.deepEqual(result, { message: '' })
61
- }
62
- })
26
+ if (process.argv[1] == __filename) {
27
+ test()
28
+ }
29
+
30
+ module.exports = test
package/Password.test.js CHANGED
@@ -5,6 +5,10 @@ const Password = require('./Password')
5
5
  const test = new Test('Password', async function () {
6
6
  const plaintext = 'MyPassword!123'
7
7
  const hashed = await Password.hash(plaintext)
8
+ await assert.rejects(
9
+ Password.verify('incorrect', hashed),
10
+ new Error('Invalid password'),
11
+ )
8
12
  await Password.verify(plaintext, hashed) // should not throw
9
13
  }).case()
10
14
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "presidium",
3
- "version": "0.16.28",
3
+ "version": "0.18.0",
4
4
  "description": "A library for creating web services",
5
5
  "author": "Richard Tong",
6
6
  "license": "MIT",