Working offline with Eclipse Che
Eclipse Che has many thousands of users. Most of them use the VS Code-inspired browser IDE, but sometimes you know you’re going to be offline for awhile and want to be able to work with a local editor, then sync back your changes when you’re back online.
Obviously if you only care about changes to the source code then git will handle this just fine. But if you want to run it offline, make changes to your tools, runtimes or dependencies then it’s great to be able to have all that synced back to Eclipse Che.
In this article we’ll cover how to set this up, by:
- Updating your Eclipse Che workspace to allow rsync.
- Importing files into your local computer/laptop.
- Edit/work with your project on your laptop
- Synchronize back all code changes from your laptop to the remote workspace.
Prerequisites
1. On my laptop: check all tools are available
-
Check local installation of
rsync
on the laptop by trying to execute the command$ rsync
. -
Check installation of
kubectl
to execute remotersync
command. -
Check installation of the
oc
CLI if Eclipse Che is running on OpenShift.
These tools will need to be on your local machine if they’re not already.
2. In Eclipse Che Workspace
The
rsync
tool needs to be part of the workspace.
Note: with OpenShift we could use
oc rsync
command. But there is no exclude filter taken into account, for
example. So in this blog post I will use only
kubectl
and
rsync
.
Your Eclipse Che workspaces may include several running
containers. (Workspace pod). Let’s add a new container that will
manage only
rsync
operations.
I will use the
rsync
tool included in the image
quay.io/fbenoit/rsync-simple
so no need to build it.
The image includes the following:
FROM alpine:latest
...
RUN apk add --no-cache rsync
...
As you can see this just adds the
rsync
tool inside the smallest container possible (based on
alpine
linux).
I’ve also added an entry-point to keep the container always-on, and an extra permissions to make it compliant with the random user-id generated by OpenShift.
Workflow
1. Updating your Eclipse Che workspace to allow rsync
The
devfile
is responsible of the definition of the Eclipse Che workspace.
Edit the
devfile
of the workspace from the Eclipse Che dashboard:
As seen in prerequisites,
rsync
tool needs to be part of the workspace and by default it’s not
there. Let’s add a new
rsync
component and let it have access to
/projects
folder using
mountSources: true
Add this component into
components
section (or create a new
components
section first if it’s not there).
Now restart the workspace to finish the inclusion of this new
rsync
component. You can check if you’ve been successful by opening the
workspace in an IDE and opening the Workspace panel (the cube icon
on the right of the IDE):
3. Importing files into your local computer/laptop
To share files on our laptop and use
rsync
,
we will need some information.
-
kubectl
ready-to-use (authentication performed) - The name of the running remote workspace pod
-
An
rsync
bash script
3.1. kubectl
In my example, I am using the online Eclipse Che instance hosted
on
che.openshift.io.
I will copy the command to be able to log into OpenShift instance.
I can use the OpenShift web console to do that. To get the web console link, open a terminal inside the IDE by using the Workspace panel within the IDE.
Open a terminal inside the Theia IDE container for example. Then type:
$ echo ${CHE\_OSO\_CLUSTER//api/console}
It will display the URL of the console:
Use command+click on
https://console.starter-us-east-2.openshift.com/
It will open link in default browser. Note that your workspace
might be assigned to a different cluster, so the URL might be
different as well.
When logged in in the OpenShift console:
- Open the top right corner link
-
Click on
copy Login command
and execute that command on your laptop.
copy the
oc
login
command and execute it on your laptop:
$ oc login https://api.starter-us-east-2.openshift.com — token=tHeEnCoDeDtOkEn
Logged into "https://api.starter-us-east-2.openshift.com:443" using the token provided.
Then use your namespace (ending with -che) using
oc project <namespace>
command:
$ oc project <namespace>-che
And we can check that the workspace pod is there by using
oc get pods
or
kubectl get pods
commands.
3.2. Name of the workspace pod
We will copy files from the remote workspace to our local machine.
We need the name of the workspace. It is available from a terminal with the following command:
$ echo $HOSTNAME
Another way is to use the
kubectl
or
oc
tool
to find the workspace pod name ($ kubectl get pods
or
$ oc get pods
).
The name of the container that includes
rsync
is
“rsync” as it’s the alias name we defined in the
devfile
.
3.3. The rsync script
To import the project files into Eclipse Che workspace, we will
now use
rsync.
We will need a shell script to perform the copy and this script
will use
kubectl exec
command.
Create a new file
kubectl-rsync.sh
with the following content:
Don’t forget to make it executable:
chmod u+x kubectl-rsync.sh
and now let’s assume you copy it to
${HOME}/bin
folder.
Now, let’s create another script named
workspace-sync.sh
Apply permissions by using
chmod u+x workspace-sync.sh
.
Copy it, for example, to
${HOME}/bin
folder. We’re using the
-e
option in rsync parameters to specify another shell.
Now that our scripts have been setup, it’s time to use them.
First, we store the workspace pod into an env variable:
$ export WORKSPACE_POD=workspacelmog4zkmikhxpc87.workspace-74d787cf95-xbd26
Let’s proceed to the first import.
Assuming our remote workspace has files in
/projects/
and that we want to copy workspace files it to
${HOME}/my-workspace
on our laptop, execute the following command:
$ RSYNC_OPTIONS="--progress --stats" RSYNC\_FROM="${WORKSPACE_POD}:/projects/" RSYNC_TO="${HOME}/my-workspace" ${HOME}/bin/workspace-sync.sh
Now files are being transferred into our laptop.
We see progress due to
--progress
option. You may add--exclude ‘node_modules'
in
RSYNC_OPTIONS
for nodejs projects to exclude unwanted files.
In your laptop you should see
my-project
folder in
${HOME}
directory.
4. Edit/work with your project on your laptop
Well here, you can do whatever you like with your project that has
been synchronized to
${HOME}/my-workspace/my-project
For example if it’s a java/maven project you can run
mvn
command there or run
yarn
for
nodejs project, etc.
5. Synchronize back all the changes
All modified files are now on your local computer so you can edit them. But at some point you’ll want those changes back in the remote Che workspace.
We’ll use the same
workspace-sync.sh
script but we’ll reverse the source and destination. Because now
we want to copy laptop’s changes to the remote workspace and not
the previous way. Also, we’ll set this time
RSYNC_INFINITE=true
to do rsync every 15s (to never miss local changes).
$ RSYNC_FROM="${HOME}/`my-workspace`/" RSYNC_TO="${WORKSPACE_POD}:/projects/" RSYNC_INFINITE="true" ${HOME}/bin/workspace-sync.sh
You can drop
--progress --stats
from
RSYNC_OPTIONS
so you don’t generate too much output now that you’ve confirmed it
works.
One issue is that when syncing back the changes, we don’t want to
upload the whole node_modules folder. Adding the
--exclude node_modules
parameter takes care of that.
RSYNC_OPTIONS="--exclude node\_modules"
The script now checks all the changes every 15 seconds.
All changes from my filesystem are now inside my remote workspace.
Let’s check:
6. Finishing
When we’re done working we can double-check that the rsync script has finished to copy all files remotely and then delete the project on our laptop.
One you have this all set up it’s easy to either leave it running all the time and fluidly move from editing on your laptop and editing in the cloud. Or only turn it on when you know you’ll be offline for awhile (like for a trip).