1- // npm modules
2- const os = require ( 'os' ) ;
3- const fs = require ( 'fs' ) ;
4- const path = require ( 'path' ) ;
5- const mkdirp = require ( 'mkdirp' ) ;
6-
71// our modules
82const { getConfig, waitForConfig} = require ( '../config' ) ;
93const docker = require ( './docker' ) ;
104const logger = require ( '../logger' ) ;
115const { initNetwork} = require ( './network' ) ;
126const { getPlugins} = require ( '../plugins' ) ;
13-
14- // config vars
15- const baseFolder = path . join ( os . homedir ( ) , '.exoframe' ) ;
16-
17- // pull image
18- const pullImage = tag =>
19- new Promise ( async ( resolve , reject ) => {
20- let log = '' ;
21- docker . pull ( tag , ( err , stream ) => {
22- if ( err ) {
23- logger . error ( 'Error pulling:' , err ) ;
24- reject ( err ) ;
25- return ;
26- }
27- stream . on ( 'data' , d => {
28- const line = d . toString ( ) ;
29- log += line ;
30- } ) ;
31- stream . once ( 'end' , ( ) => resolve ( log ) ) ;
32- } ) ;
33- } ) ;
34- exports . pullImage = pullImage ;
7+ const { initTraefik} = require ( './traefik' ) ;
358
369// export default function
3710exports . initDocker = async ( ) => {
@@ -44,20 +17,6 @@ exports.initDocker = async () => {
4417 // get config
4518 const config = getConfig ( ) ;
4619
47- // check if traefik management is disabled
48- if ( ! config . traefikImage ) {
49- logger . info ( 'Traefik managment disabled, skipping init..' ) ;
50- return ;
51- }
52-
53- // build acme path
54- const acmePath = path . join ( baseFolder , 'traefik' , 'acme' ) ;
55- try {
56- fs . statSync ( acmePath ) ;
57- } catch ( e ) {
58- mkdirp . sync ( acmePath ) ;
59- }
60-
6120 // run init via plugins if available
6221 const plugins = getPlugins ( ) ;
6322 logger . debug ( 'Got plugins, running init:' , plugins ) ;
@@ -75,102 +34,6 @@ exports.initDocker = async () => {
7534 }
7635 }
7736
78- // get all containers
79- const allContainers = await docker . listContainers ( { all : true } ) ;
80- // try to find traefik instance
81- const traefik = allContainers . find ( c => c . Names . find ( n => n . startsWith ( `/${ config . traefikName } ` ) ) ) ;
82-
83- // if traefik exists and running - just return
84- if ( traefik && ! traefik . Status . includes ( 'Exited' ) ) {
85- logger . info ( 'Traefik already running, docker init done!' ) ;
86- return ;
87- }
88-
89- // if container is exited - remove and recreate
90- if ( traefik && traefik . Status . startsWith ( 'Exited' ) ) {
91- logger . info ( 'Exited traefik instance found, re-creating...' ) ;
92- const traefikContainer = docker . getContainer ( traefik . Id ) ;
93- // remove
94- await traefikContainer . remove ( ) ;
95- }
96-
97- // pull image if needed
98- const allImages = await docker . listImages ( ) ;
99- const traefikImage = allImages . find ( img => img . RepoTags && img . RepoTags . includes ( config . traefikImage ) ) ;
100- if ( ! traefikImage ) {
101- logger . info ( 'No traefik image found, pulling..' ) ;
102- const pullLog = await pullImage ( config . traefikImage ) ;
103- logger . debug ( pullLog ) ;
104- }
105-
106- // debug flags
107- const debug = [ '--debug' , '--logLevel=DEBUG' ] ;
108-
109- // letsencrypt flags
110- const letsencrypt = [
111- '--acme' ,
112- `--acme.email=${ config . letsencryptEmail } ` ,
113- '--acme.storage=/var/acme/acme.json' ,
114- '--acme.httpchallenge.entrypoint=http' ,
115- '--acme.entrypoint=https' ,
116- '--acme.onhostrule=true' ,
117- '--accesslogsfile=/var/acme/access.log' ,
118- `--entryPoints=Name:https Address::443 TLS ${ config . compress ? 'Compress:on' : 'Compress:off' } ` ,
119- `--entryPoints=Name:http Address::80 Redirect.EntryPoint:https ${ config . compress ? 'Compress:on' : 'Compress:off' } ` ,
120- '--defaultEntryPoints=https,http' ,
121- ] ;
122-
123- // entrypoints without letsencrypt
124- const entrypoints = [
125- `--entryPoints=Name:http Address::80 ${ config . compress ? 'Compress:on' : 'Compress:off' } ` ,
126- '--defaultEntryPoints=http' ,
127- ] ;
128-
129- // construct command
130- const Cmd = [
131- '-c' ,
132- '/dev/null' ,
133- '--docker' ,
134- '--docker.watch' ,
135- ...( config . letsencrypt ? letsencrypt : entrypoints ) ,
136- ...( config . debug ? debug : [ ] ) ,
137- ...( config . traefikArgs || [ ] ) ,
138- ] ;
139-
140- const Labels = {
141- 'exoframe.deployment' : 'exo-traefik' ,
142- 'exoframe.user' : 'admin' ,
143- } ;
144-
145- const RestartPolicy = {
146- Name : 'on-failure' ,
147- MaximumRetryCount : 2 ,
148- } ;
149-
150- // start traefik in docker
151- const container = await docker . createContainer ( {
152- Image : config . traefikImage ,
153- name : config . traefikName ,
154- Cmd,
155- Labels,
156- ExposedPorts : {
157- '80/tcp' : { } ,
158- '443/tcp' : { } ,
159- } ,
160- HostConfig : {
161- RestartPolicy,
162- Binds : [ '/var/run/docker.sock:/var/run/docker.sock' , `${ acmePath } :/var/acme` ] ,
163- PortBindings : {
164- '80/tcp' : [ { HostPort : '80' } ] ,
165- '443/tcp' : [ { HostPort : '443' } ] ,
166- } ,
167- } ,
168- } ) ;
169- // connect traefik to exoframe net
170- await exoNet . connect ( {
171- Container : container . id ,
172- } ) ;
173- // start container
174- await container . start ( ) ;
175- logger . info ( 'Traefik instance started..' ) ;
37+ // run traefik init
38+ await initTraefik ( exoNet ) ;
17639} ;
0 commit comments