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 #12 from janithcooray/sync-change
Browse files Browse the repository at this point in the history
Sync change
  • Loading branch information
janithcooray authored Jun 30, 2022
2 parents 73878cf + be776e3 commit e64b83d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 74 deletions.
127 changes: 57 additions & 70 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,75 +1,53 @@
# Docker Volume Sync for Faster Storage Sync
---
## Pleast note that this is a pre-release, functionality is still partial! <br> Wait for a newer update
# Docker Volume Sync for Faster Development on MacOS and Windows

## Pleast note that this is a pre-release, functionality is still partial! <br> Wait for a newer update!!

[![CodeQL](https://github.com/janithcooray/sync-stat/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/janithcooray/sync-stat/actions/workflows/codeql-analysis.yml)
[![Node.js Package](https://github.com/janithcooray/sync-stat/actions/workflows/npm-publish.yml/badge.svg)](https://github.com/janithcooray/sync-stat/actions/workflows/npm-publish.yml)
---
## WTF is this?

---
## What it does

## Installation
sync-stat is an NPX CLI toolkit that offers an alternative to Docker Bind Mounts that is on par with Bind mounts on Linux Based FSs in terms of performance for MacOS.(Windows NTFS support is comming soon)

### Install via NPM
```sh
npm install sync-stat
```

post installtion - add script to package.json

```sh
node node_modules/sync-stat/index -m install
```
Docker with bind mounts can really cause a major performance penalty in MacOS (APPFS) and windows (NTFS)

## Configure - create `sync-compose.json`
Windows has WSL so it's not really effected but it would be nice if it would work as fast as bind mounts in linux in NTFS

create a JSON file named `sync-compose,json` at the root directory of your project

sync-stat also offers some other options that docker cp / bind mounts do not offer. such as the ability to set an explicit user group and mode to the directory

### Basic config
---

sync every change from ./app/* to container-name:/var/www/html
## Installation

```json
{
"containers":{
"container-name":{
"/path/to/container":"app"
}
}
}
### Install via NPM
```sh
npm i sync-stat
```

## Configure - create `sync-compose.yaml`

create a YAML file named `sync-compose.yaml` at the root directory of your project

```yaml
version: 1

containers: #Array
- name-of-container: #Array - must be defined in docker-compose or name container on start
volumes: # Bind Mounts - Array
- volume: # Item - config for each bind
from: app/ # From location relative to <ProjectRoot> => app/ || does not support ./app yet
to: /var/www/html/ # to location inside docker container
mode: 775 # Mode to write files
owner: www-data:www-data # owner to write files as
cmd: #Run the Following commands on start, this only runs 1 time
- npm install
- composer install
ignore: #ignore these directories when syncing
- node_modules/
replace: # replace these strings when copying
- "localhost:mysql-db"

### Multiple Containers

to add multiple containers, add the 2nd container name after 1st container

```json
{
"containers":{
"container-name":{
"/path/to/container":"app"
},
"another-container-name":{
"/another/path":"same/path"
},
}
}
```

### Multiple Volumes per Container

```json
{
"containers":{
"container-name":{
"/path/to/container":"app",
"/another/path/to/container":"another/folder"
}
}
}
```

## Example
Expand All @@ -82,24 +60,33 @@ services:
wordpress:
container_name: mytest-container
image: some-image #REPLACE
volumes:
volumes:
- ./app:/var/www/html # (IMPORTANT!) REMOVE THIS. sync-stat will Automaticall do this
# MOUNT CANNOT MATCH THE ONE IN SYNC-COMPOSE
# IF YOU DONT REMOVE THAT LINE, IT WILL CAUSE AN ERROR.
ports:
- "8000:80"
## application is placed at /var/www/html
```
sync-compose.json

```json
{
"containers":{
"mytest-container":{
"/var/www/html":"app"
}
}
}
sync-compose.yaml
```yaml
version: 1

containers:
- mytest-container:
volumes:
- volume:
from: app/
to: /var/www/html/
mode: 775
owner: www-data:www-data
cmd:
- echo hi
ignore:
- node_modules/
replace:
- "string:string"

```


Expand All @@ -110,7 +97,7 @@ sync-compose.json
make sure to name the container with matching names defined in the sync-compose.json

### Start Sync

Once Containers are online run the following in the root directory
```sh
npm run sync-stat
npx sync-stat run
```
7 changes: 5 additions & 2 deletions src/ymlProcess/watchChange.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ export default class WatchChange extends Log {
this.containerName = volume.container;
this.volumePath = volume.to;
this.fromPath = volume.from;
this.owner = volume.owner;
}


async startSync() {
chokidar.watch(this.fromPath,{ignoreInitial: true,usePolling: false}).on('all', (event, path) => {
if (event=="change" || event == "add") {
console.log('copy '+path + " to " + this.containerName +":"+ this.dockerpath(path));
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)));
console.log('✅ copied '+path + " to " + this.containerName +":"+ this.dockerpath(path));
} catch (error) {
console.log(this.getPath(path));
console.log("❌ unable to copy"+this.dockerpath(this.getPath(path)));
}
}
if (event=="addDir") {
Expand Down
4 changes: 2 additions & 2 deletions src/ymlProcess/yamlProcess.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import WatchChange from "./watchChange.js";
this.yml = new LoadYML();
this.containers = this.getContainers();
this.volumes = this.getVolumes(this.containers);
this.output("# of sync ops "+this.volumes.length);
this.output("🚀 no. of sync ops "+this.volumes.length);
this.startSync(this.volumes);
}

Expand All @@ -38,7 +38,7 @@ import WatchChange from "./watchChange.js";
startSync(volumes){
volumes.forEach(element => {
let changes = new WatchChange(element);
this.output(element.container+" will be synced on "+element.from + ":"+element.to )
this.output("🚀 "+element.container+" will be synced on "+element.from + ":"+element.to )
changes.startSync()
});
}
Expand Down

0 comments on commit e64b83d

Please sign in to comment.