In order to improve my telecommuting efficiency, I spent less than a day developing a code synchronization tool to synchronize code between multiple remote servers.
Address: github.com/nladuo/sync…
Aimed at the scene
Recently, due to the pneumonia outbreak, everyone is working remotely. My usual development tool is terminal +PyCharm.
The terminal connects to the server, runs the command, and uploads the code to the server in real time through PyCharm, which looks something like this:
However, the servers are all Intranet, so connecting to the server at home requires configuring the proxy, and if I am not in the lab, I have to go through 2-3 more SSH sessions if I connect to server 1. For termnial, I’m going to use SSH twice.
But for Termnial, I can run a TMUX on my own cloud server, which is no different from operating in a lab.
However, for code editing, SINCE I am not used to using Vim personally, if I use PyCharm, THEN I need two more SCP, which is obviously not what I want.
Why the wheel
Scrolling through Github, I found one such tool: github.com/serkanyerse… It seems to meet my needs. Its effect is as follows:
However, after using this tool, I found that it is actually a local tool, which is not suitable for such scenarios with high network latency. If I use this tool in my scenario, I found that the size of most files uploaded is 0.
Since it can take several seconds to transfer a 1 Megabyte file across the network, the event that the file was written will continue to be triggered for several seconds. So it’s probably going to be uploading all the time.
In addition, for network file transfer, first to create an empty file, and then write to the empty file, the two time interval is very short.
// create destination file
dstFile, err := sftpClient.Create(remotepath)
iferr ! =nil {
log.Fatal(err)
}
defer dstFile.Close()
// create source file
srcFile, err := os.Open(localpath)
iferr ! =nil {
log.Fatal(err)
}
// copy source file to destination file
_, err = io.Copy(dstFile, srcFile)
iferr ! =nil {
log.Fatal(err)
}
Copy the code
A tool like the one above may receive a file creation signal and create a new file, but it is actually followed by a write operation. If the network speed is ok, below the minimum response time of the software, the write operation may be ignored.
So instead of the local to remote server synchronization tool, I added the following two synchronization tools between remote servers:
-
- Add some latency to prevent files from being written more than once.
-
- For each file modification, if the file size is larger than a certain extent (for example, 20KB), you need to continuously check whether the file is being written. You need to wait for the file to be written before uploading it.
Method of use
Download and install
Since I’m using go, I can download the compiled results directly. And then add it to path.
Wget https://github.com/nladuo/sync-go/releases/download/1.0/sync-go chmod + x sync - go mv sync - go/usr /local/bin/
Copy the code
Only linux-64-bit compilation results are provided here, since this is certainly used between Linux, and 32-bit servers should be rare these days.