This is tooling to provide a fast and convenient way to spin up a shell in the cloud.
- Custom shell image with Packer and Ansible
- Easily customise the image with additional Ansible Galaxy roles
- Edit files on the remote machine using rmate for TextMate 2
- Forward local ssh key for use with git
The image must first be built and stored in the desired GCP zone.
Prerequisites:
- gcloud must be installed and set to an account with the
roles/owner
role. - Ansible must be installed. See here for full installation instructions.
- Mac OSX:
brew install ansible
- Mac OSX:
Build the image:
-
Edit cloud-shell.cfg and set your GCP project ID and zone. Other configuration is optional.
PROJECT_ID="cloud-shell-215704" ZONE="asia-northeast1-a"
-
Execute the following script to create a Google Cloud service account for Packer. It will be created with the
compute.instanceAdmin.v1
andiam.serviceAccountUser
roles../create_packer_service_account.sh
-
(Optional) Customise the shell image by, for example, adding Ansible Galaxy roles.
-
Prepare the required ansible roles locally.
ansible-galaxy install -r requirements.yml
-
Build and store the image in GCP.
./build.sh
Launch a shell:
./cloud-shell.sh up
SSH to the virtual machine:
./cloud-shell.sh ssh
Delete the virtual machine:
./cloud-shell.sh down
The default image comes with rmate
installed allowing editing of files on the remote machine.
On your local machine you need to have an extension/plugin for your editor that supports Textmate's 'rmate' feature. Here are a couple of options:
- Visual Studio Code: Remote VSCode
- Sublime Text: RemoteSubl
- Install the
Remote VSCode
extension. - Open the command palette (CTRL+P for Windows and CMD+P for Mac) then execute the
>Remote: Start Server
command. - SSH to the remote machine with
./cloud-shell.sh ssh
- Open remote files in Visual Studio Code with the command
rmate my-file.txt
This will allow use of your local SSH keys by the remote machine. For example, if you have an SSH key that you have authorised access to your GitHub account it will be usable on the remote machine.
Add the following configuration to ~/.ssh/config
if you would like your local ssh key to be forwarded to the remote host.
The <ip address>
is the IP of the virtual machine displayed after executing ./cloud-shell.sh up
.
Host <ip address>
ForwardAgent yes
Once connected via ssh test the connection to GitHub:
ssh -T [email protected]
The following error message when building is due to the Too many SSH keys issue.
googlecompute: fatal: [default]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Warning: Permanently added '[127.0.0.1]:54452' (RSA) to the list of known hosts.\r\nReceived disconnect from 127.0.0.1 port 54452:2: too many authentication failures\r\nAuthentication failed.\r\n", "unreachable": true}
It can be fixed by clearing all keys from your ssh-agent.
$ ssh-add -D
MIT License - see the LICENSE file for details