Skip to content
This repository has been archived by the owner on Feb 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #14 from janithcooray/sync-change
Browse files Browse the repository at this point in the history
Sync change
  • Loading branch information
janithcooray authored Jul 1, 2022
2 parents e64b83d + b53a007 commit b6a7e48
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class Init extends Log {

switch (args_[0]) {
case "run":
new ProcessLinkYml();
new ProcessLinkYml(args_);
break;
case "convert":
new Convert();
Expand Down Expand Up @@ -44,6 +44,7 @@ this.output(
`Commands: [Option] <ARG1> <ARG2> ...
run - run sync compose
db - export db copy from server and import to local
help,-h , --help - display this help message
convert - convert an existing docker-compose.yml
version - Display Version
Expand Down
4 changes: 2 additions & 2 deletions src/processYML.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import yaml from "js-yaml";
import ProcessYML from "./ymlProcess/yamlProcess.js";
export default class ProcessLinkYml extends Log {

constructor(){
constructor(arg){
super();
new ProcessYML();
new ProcessYML(arg);
}
}
4 changes: 2 additions & 2 deletions src/ymlProcess/volumeData.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default class VolumeInfo {
this.owner = object.owner,
this.cmd = object.cmd,
this.ignore = object.ignore,
this.replace = object.replace

this.replace = object.replace,
this.profile = object.profile
}
}
37 changes: 35 additions & 2 deletions src/ymlProcess/watchChange.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ export default class WatchChange extends Log {
this.volumePath = volume.to;
this.fromPath = volume.from;
this.owner = volume.owner;
this.cmd = volume.cmd;
this.mode = volume.mode;
}



async startSync() {
chokidar.watch(this.fromPath,{ignoreInitial: true,usePolling: false}).on('all', (event, path) => {
if (event=="change" || event == "add") {
try {
child_process.execSync('docker exec ' + this.containerName +' mkdir -p '+this.dockerpath(this.getPath(path)));
child_process.execSync('docker cp "'+path+'" ' + this.containerName +':'+this.dockerpath(path));
child_process.execSync('docker exec ' + this.containerName +' chown -R '+this.owner+' '+this.dockerpath(this.getPath(path)));
child_process.execSync('docker exec ' + this.containerName +' chmod -R '+this.mode+' '+this.dockerpath(this.getPath(path)));
//child_process.execSync('docker exec ' + this.containerName +' chown -R '+this.owner+' '+this.dockerpath(this.getPath(path)));
//child_process.execSync('docker exec ' + this.containerName +' chmod -R '+this.mode+' '+this.dockerpath(this.getPath(path)));
console.log('✅ copied '+path + " to " + this.containerName +":"+ this.dockerpath(path));
} catch (error) {
console.log("❌ unable to copy"+this.dockerpath(this.getPath(path)));
Expand All @@ -39,10 +42,40 @@ export default class WatchChange extends Log {
});
}

up(){
this.output("🚀 Copying Source to "+ this.containerName);
child_process.execSync('docker cp "'+this.dcpeParse(this.fromPath)+'" ' + this.containerName +':'+this.volumePath);
console.log('✅ Ok');

this.output("🚀 Changing Destination Owner for "+ this.containerName);
child_process.execSync('docker exec ' + this.containerName +' chown -R '+this.owner+' '+this.volumePath);
console.log('✅ Ok');

this.output("🚀 Changing Destination Mode for "+ this.containerName);
child_process.execSync('docker exec ' + this.containerName +' chmod -R '+this.mode+' '+this.volumePath);
console.log('✅ Ok');

this.output("✅ Sync Ready for "+this.containerName);

if(this.cmd != null){
this.cmd.forEach(element => {
this.output("🚀 "+ this.containerName+ " "+element);
child_process.execSync('docker exec ' + this.containerName +" "+ element);
console.log('✅ Ok');
});
}

this.startSync();
}

dockerpath(params) {
//this.output(params)
return '"'+params.replace(this.fromPath,this.volumePath)+'"';
}

dcpeParse(path){
return path.endsWith("/")?path+".":path;
}

getPath(params) {
let pieces = params.split('/')
Expand Down
22 changes: 18 additions & 4 deletions src/ymlProcess/yamlProcess.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import WatchChange from "./watchChange.js";

export default class ProcessYML extends Log {

constructor(){
constructor(arg){
super();
this.yml = new LoadYML();
this.containers = this.getContainers();
this.volumes = this.getVolumes(this.containers);
this.profile = arg[1];
this.output("🚀 no. of sync ops "+this.volumes.length);
this.startSync(this.volumes);
}
Expand All @@ -34,12 +35,25 @@ import WatchChange from "./watchChange.js";
return allVolumes;
}

isInProfile(profile){
if (this.profile != null) {
if (profile == this.profile || profile== null ) {
return true;
}
else{
return false;
}
}
return true;
}

startSync(volumes){
volumes.forEach(element => {
let changes = new WatchChange(element);
this.output("🚀 "+element.container+" will be synced on "+element.from + ":"+element.to )
changes.startSync()
if (this.isInProfile(element.profile)) {
let changes = new WatchChange(element);
this.output("🚀 "+element.container+" will be synced on "+element.from + ":"+element.to )
changes.up()
}
});
}

Expand Down
40 changes: 40 additions & 0 deletions sync-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,43 @@ containers:
- node_modules/
replace:
- "string:string"
- volume:
profile: local
from: test/config.php
to: /var/www/html/config.php
mode: 775
owner: www-data:www-data
cmd:
- echo local
ignore:
- node_modules/
replace:
- "string:string"
- volume:
profile: staging
from: test/test/sample.php
to: /var/www/html/sample.php
mode: 775
owner: www-data:www-data
cmd:
- echo staging
ignore:
- node_modules/
replace:
- "string:string"

database:
driver: mysql
server:
database_user:
database_name:
database_pass:
host:
post:
local:
database_user:
database_name:
database_pass:
root_password:
host:
post:
2 changes: 1 addition & 1 deletion test/index.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php
echo "hithere";
echo "s";
?>

0 comments on commit b6a7e48

Please sign in to comment.