Compare commits

..

8 Commits

Author SHA1 Message Date
0aab639fbd 1.0.68 2019-09-13 22:43:30 +02:00
794bb60dfc fix(core): update 2019-09-13 22:43:29 +02:00
b182a379af 1.0.67 2019-09-13 22:37:38 +02:00
5c6c06dee6 fix(core): update 2019-09-13 22:37:38 +02:00
a48e1e035e 1.0.66 2019-09-13 22:31:03 +02:00
8836c06b56 fix(core): update 2019-09-13 22:31:03 +02:00
7af8e0739b 1.0.65 2019-09-13 22:09:35 +02:00
684185e951 fix(core): update 2019-09-13 22:09:35 +02:00
7 changed files with 54 additions and 22 deletions

2
package-lock.json generated
View File

@ -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": {

View File

@ -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
View 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();

View File

@ -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"}`);

View File

@ -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,

View File

@ -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> {

View File

@ -10,4 +10,5 @@ export interface IServiceCreationDescriptor {
networks: DockerNetwork[]; networks: DockerNetwork[];
networkAlias: string; networkAlias: string;
secrets: DockerSecret[]; secrets: DockerSecret[];
ports: string[];
} }