Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
0aab639fbd | |||
794bb60dfc | |||
b182a379af | |||
5c6c06dee6 | |||
a48e1e035e | |||
8836c06b56 | |||
7af8e0739b | |||
684185e951 |
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@mojoio/docker",
|
"name": "@mojoio/docker",
|
||||||
"version": "1.0.64",
|
"version": "1.0.68",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@mojoio/docker",
|
"name": "@mojoio/docker",
|
||||||
"version": "1.0.64",
|
"version": "1.0.68",
|
||||||
"description": "easy communication with docker remote api from node, TypeScript ready",
|
"description": "easy communication with docker remote api from node, TypeScript ready",
|
||||||
"private": false,
|
"private": false,
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
11
scripts/testauth.ts
Normal file
11
scripts/testauth.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import * as docker from '../ts';
|
||||||
|
import * as smartstring from '@pushrocks/smartstring';
|
||||||
|
|
||||||
|
const run = async () => {
|
||||||
|
const dockerHost = new docker.DockerHost();
|
||||||
|
await docker.DockerImage.createFromRegistry(dockerHost, {
|
||||||
|
imageUrl: 'registry.gitlab.com/servezone/private/cloudly:latest'
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
run();
|
@ -103,7 +103,8 @@ tap.test('should create a service', async () => {
|
|||||||
name: 'testService',
|
name: 'testService',
|
||||||
networks: [testNetwork],
|
networks: [testNetwork],
|
||||||
networkAlias: 'testService',
|
networkAlias: 'testService',
|
||||||
secrets: [testSecret]
|
secrets: [testSecret],
|
||||||
|
ports: []
|
||||||
});
|
});
|
||||||
|
|
||||||
await testSecret.update(`{"updated": "socool"}`);
|
await testSecret.update(`{"updated": "socool"}`);
|
||||||
|
@ -3,6 +3,12 @@ import { DockerContainer } from './docker.classes.container';
|
|||||||
import { DockerNetwork } from './docker.classes.network';
|
import { DockerNetwork } from './docker.classes.network';
|
||||||
import { DockerService } from './docker.classes.service';
|
import { DockerService } from './docker.classes.service';
|
||||||
|
|
||||||
|
export interface IAuthData {
|
||||||
|
serveraddress: string;
|
||||||
|
username: string;
|
||||||
|
password: string;
|
||||||
|
}
|
||||||
|
|
||||||
export class DockerHost {
|
export class DockerHost {
|
||||||
/**
|
/**
|
||||||
* the path where the docker sock can be found
|
* the path where the docker sock can be found
|
||||||
@ -31,26 +37,16 @@ export class DockerHost {
|
|||||||
* @param userArg
|
* @param userArg
|
||||||
* @param passArg
|
* @param passArg
|
||||||
*/
|
*/
|
||||||
public async auth(registryUrl: string, userArg: string, passArg: string) {
|
public async auth(authData: IAuthData) {
|
||||||
const response = await this.request('POST', '/auth', {
|
const response = await this.request('POST', '/auth', authData);
|
||||||
serveraddress: registryUrl,
|
|
||||||
username: userArg,
|
|
||||||
password: passArg
|
|
||||||
});
|
|
||||||
if (response.body.Status !== 'Login Succeeded') {
|
if (response.body.Status !== 'Login Succeeded') {
|
||||||
console.log(`Login failed with ${response.body.Status}`);
|
console.log(`Login failed with ${response.body.Status}`);
|
||||||
throw new Error(response.body.Status);
|
throw new Error(response.body.Status);
|
||||||
}
|
}
|
||||||
console.log(response.body.Status);
|
console.log(response.body.Status);
|
||||||
this.registryToken = plugins.smartstring.base64.encode(response.body.IdentityToken);
|
this.registryToken = plugins.smartstring.base64.encode(
|
||||||
}
|
plugins.smartjson.Smartjson.stringify(authData, {})
|
||||||
|
);
|
||||||
/**
|
|
||||||
* sets an auth token
|
|
||||||
* @param authToken
|
|
||||||
*/
|
|
||||||
public setAuthToken(authToken: string) {
|
|
||||||
this.registryToken = authToken;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -59,7 +55,14 @@ export class DockerHost {
|
|||||||
public async getGitlabComTokenFromDockerConfig() {
|
public async getGitlabComTokenFromDockerConfig() {
|
||||||
const dockerConfigPath = plugins.smartpath.get.home('~/.docker/config.json');
|
const dockerConfigPath = plugins.smartpath.get.home('~/.docker/config.json');
|
||||||
const configObject = plugins.smartfile.fs.toObjectSync(dockerConfigPath);
|
const configObject = plugins.smartfile.fs.toObjectSync(dockerConfigPath);
|
||||||
this.registryToken = configObject.auths['registry.gitlab.com'].auth;
|
const gitlabAuthBase64 = configObject.auths['registry.gitlab.com'].auth;
|
||||||
|
const gitlabAuth: string = plugins.smartstring.base64.decode(gitlabAuthBase64);
|
||||||
|
const gitlabAuthArray = gitlabAuth.split(':');
|
||||||
|
await this.auth({
|
||||||
|
username: gitlabAuthArray[0],
|
||||||
|
password: gitlabAuthArray[1],
|
||||||
|
serveraddress: 'registry.gitlab.com'
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -170,6 +173,7 @@ export class DockerHost {
|
|||||||
method: methodArg,
|
method: methodArg,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
|
'X-Registry-Auth': this.registryToken,
|
||||||
Host: 'docker.sock'
|
Host: 'docker.sock'
|
||||||
},
|
},
|
||||||
requestBody: null,
|
requestBody: null,
|
||||||
|
@ -58,6 +58,18 @@ export class DockerService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const ports = [];
|
||||||
|
for (const port of serviceCreationDescriptor.ports) {
|
||||||
|
const portArray = port.split(':');
|
||||||
|
const hostPort = portArray[0];
|
||||||
|
const containerPort = portArray[1];
|
||||||
|
ports.push({
|
||||||
|
"Protocol": "tcp",
|
||||||
|
"PublishedPort": containerPort,
|
||||||
|
"TargetPort": hostPort
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const secretArray: any[] = [];
|
const secretArray: any[] = [];
|
||||||
for (const secret of serviceCreationDescriptor.secrets) {
|
for (const secret of serviceCreationDescriptor.secrets) {
|
||||||
secretArray.push({
|
secretArray.push({
|
||||||
@ -89,8 +101,11 @@ export class DockerService {
|
|||||||
},
|
},
|
||||||
ForceUpdate: 1
|
ForceUpdate: 1
|
||||||
},
|
},
|
||||||
Labels: serviceCreationDescriptor.labels,
|
Labels: labels,
|
||||||
Networks: networkArray
|
Networks: networkArray,
|
||||||
|
EndpointSpec: {
|
||||||
|
Ports: ports
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const createdService = await DockerService.getServiceByName(
|
const createdService = await DockerService.getServiceByName(
|
||||||
@ -161,7 +176,7 @@ export class DockerService {
|
|||||||
|
|
||||||
public async reReadFromDockerEngine() {
|
public async reReadFromDockerEngine() {
|
||||||
const dockerData = await this.dockerHostRef.request('GET', `/services/${this.ID}`);
|
const dockerData = await this.dockerHostRef.request('GET', `/services/${this.ID}`);
|
||||||
Object.assign(this, dockerData);
|
// TODO: Better assign: Object.assign(this, dockerData);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async needsUpdate(): Promise<boolean> {
|
public async needsUpdate(): Promise<boolean> {
|
||||||
|
@ -10,4 +10,5 @@ export interface IServiceCreationDescriptor {
|
|||||||
networks: DockerNetwork[];
|
networks: DockerNetwork[];
|
||||||
networkAlias: string;
|
networkAlias: string;
|
||||||
secrets: DockerSecret[];
|
secrets: DockerSecret[];
|
||||||
|
ports: string[];
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user