geoschem / geos-chem-cloud

Run GEOS-Chem easily on AWS cloud
http://cloud.geos-chem.org
MIT License
39 stars 9 forks source link

[BUG/ISSUE] Git Bash setup for MS Windows #17

Open yantosca opened 5 years ago

yantosca commented 5 years ago

I was able to use Git Bash from MS Windows 7 to log into the AWS instance. However, on Windows there are a couple of assumptions and extra steps:

(1) Git-Bash on Windows assumes that your home directory is

C:/Users/YOUR-WINDOWS-NAME 

where YOUR-WINDOWS-NAME is your screen name on Windows.

(2) Git-Bash on Windows assumes that your SSH keys will be stored by default in

C:/Users/YOUR-WINDOWS-NAME/.ssh

So this is the place where you can put the private key that you created to log into AWS.

(3) Git-Bash on Windows requires that this .bashrc be placed in your home directory (i.e. C:/Users/YOUR-WINDOWS-NAME):

#======================================================
# Set personal preferences (feel free to edit)
#======================================================

# Change to home directory
cd C:/Users/YOUR-WINDOWS-NAME

# Name of the private key you created for AWS
aws_priv_key=C:/Users/YOUR-WINDOWS-NAME/.ssh/YOUR-AWS-PRIVATE-KEY

# Set prompt (optional)
PS1="\[\e[1;93m\][YOUR-WINDOWS-NAME \W]$\[\e[0m\] "

# Launch an AWS setting with the AWS private key you set up
alias login_aws="ssh -XA -i ${aws_priv_key} "

#======================================================
# Start the ssh agent and add your AWS private key
#======================================================
env=~/.ssh/agent.env

agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }

agent_start () {
     (umask 077; ssh-agent >| "$env")
    . "$env" >| /dev/null ; }

agent_load_env

# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)

if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
    agent_start
    ssh-add ${aws_priv_key}
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
    ssh-add ${aws_priv_key}
fi

unset env

This will make sure to forward the private key to your AWS instance. Replace YOUR-WINDOWS-NAME your Windows screen name and YOUR-AWS-PRIVATE-KEY with the name of the private key you made to log in to AWS.

(4) Make sure to add public key that corresponds to your AWS private key to Github, or else you won't be able to clone repos from there.

JiaweiZhuang commented 5 years ago

Thanks for the notes! The reason why I recommend Git-BASH is that port-forwarding is straightforward (just -L like on Unix). Doing so in MobaXterm is fairly complicated.

yantosca commented 5 years ago

So Git-Bash by itself won't work for MS Windows. You have to also start an instance of MobaXterm, which supplies the X11 forwarding. (Windows doesn't have an X11 server, but MacOS does.) You also have to turn on this MobaXterm setting: Settings -> Configuration -> Terminal -> Use Windows PATH environment

And you have to add these additional lines to the .bashrc in the post above (in the "Set personal preferences" section):

# Tell Git-Bash where to find the X11 server (change to your setings)
export PATH=$PATH:/c/Users/YOUR-WINDOWS-NAME/Documents/MobaXterm

# Set the default display variable
export DISPLAY=localhost:0

Then when you start a new Git-bash window, you will be able to pop open new windows like xterm, emacs.

Maybe we can add a section to the cloud-geos-chem.org tutorial for Git-bash from windows (an appendix?). I am sure there will be several users who will try to log on to AWS via Windows machines.

lizziel commented 5 years ago

Some people might already have PuTTY/Xming so we could put that as an option as well.

lizziel commented 5 years ago

Regarding: "Make sure to add public key that corresponds to your AWS private key to Github, or else you won't be able to clone repos from there."

I have not had to add an AWS private key to Github (or bitbucket) to clone public repos after ssh'ing from a Mac. Even for private repos, or for pushing, I do not have to do this (although it is nice for convenience) since I am prompted for my password. Are you positive this is a requirement after ssh'ing from Windows, and if yes, do you know why?

yantosca commented 5 years ago

If you use the same private key for AWS as you do for logging into e.g. Odyssey, and that key is already on Github, then you don't need to use an extra key.

Just if people use a different private key for AWS, then the corresponding public key also has to be added to Github.

JiaweiZhuang commented 5 years ago

So Git-Bash by itself won't work for MS Windows.

You mean the x11 window doesn't work? I actually never use x11 forwarding on EC2; when I need a graphic interface for git I would simply use jupyterlab-git. Cannot think of other cases that need x11 window...

Some people might already have PuTTY/Xming

Again the port forwarding would be more complicated to set up.

I am recommending Git-BASH so that users can use Jupyter as quickly as possible (https://github.com/geoschem/geos-chem-cloud/issues/15#issuecomment-426680167). Doing so in other software is a lot more time-consuming.

If users want to use x11 window, they can fall back to their original terminal software like MobaXterm.

In the guide I have mentioned:

Alternatively, you can use MobaXterm, Putty, Linux Subsystem or PowerShell with OpenSSH.

For each software I provided a link to a more detailed instruction.

yantosca commented 5 years ago

OK some clarification...

If you use https method with Github, then you don't need to add any public keys to your Github account. Then when you clone, you just type your username & passweord. If you are using the https method, then you can ignore my comment above about copying the public key that corresponds to your AWS private key to github.

But if you are like me and you do you use the ssh method with Github, you will definitely need to set up the ssh-agent forwarding AND copy a public key to Github as described in the prior comments. If you don't , you won't be able to clone.

Each user can decide whether to use https or ssh with Github. As @lizziel and I just found out, Github recommends using https, but only because that is easier for new users to understand. But it's not because the https method is intrinsically better than ssh. In some instances (e.g. if you are behind a firewall) then the https method might not work, so you would have to use the ssh method.

So we can put all of this info in the documentation. Maybe as an appendix for Git-bash users and windows. Note that there is no explicit requirement for adding a SSH key to Github. But for those who use the SSH method, they will have to take some extra steps.

I personally think it's a good idea to use the .bashrc with the agent-forwarding for Git-Bash on Windows. Then you can also ssh out of EC2 to another site e.g. if you had to grab external data from somewhere else.

These are precisely the types of questions we would get from GEOS-Chem users, so I'm glad we are thinking about this...

JiaweiZhuang commented 5 years ago

Thanks for the clarification, I see the points regarding Github!

These are precisely the types of questions we would get from GEOS-Chem users, so I'm glad we are thinking about this...

Also glad that these discussions are publicly on issue tracker so that we can simply point users to here😂

FeiYao-Edinburgh commented 5 years ago

Thanks for your attention and discussion! I have solved the Git Bash problem based on your comments. Or more honestly, Git Bash should not be a problem because I previously confused my machines and AWS servers. If my memory is correct, I logged into a Linux machine under a window local machine (MS Windows 10) and then logged into the AWS server under that Linux machine. Consequently, I cannot open the http://localhost:8999/ under the windows machine but can open it under the Linux machine (I have told it to Jiawei). Therefore this time, I logged into the AWS server under the windows machine directly and no problem anymore! I apologize that my mistake caused you more work but I, hopefully, believe that the discussion here could be helpful for those MobaXterm users and the like. Thanks for all of your work again!

RajMLal commented 4 years ago

Hi all, I’m trying to set up the .bashrc file to launch an instance from git-bash, but I’m running into some issues. First, I created a .bashrc file following the script provided: https://cloud-gc.readthedocs.io/en/latest/chapter03_advanced-tutorial/advanced-awscli.html

pic1

When I ran that file in git-bash, it gave me the following errors:

pic2

I copied, pasted, and edited the commands Bob posted on the initial post (Comment (3)) but it still gave me the same error:

pic3

Do you have any thoughts on how to correct these problems? Do I have to do anything in the AWS console before running the .bashrc file?

Thank you all again for your time and support, Raj

yantosca commented 4 years ago

Hi Raj. Also see this issue: https://github.com/geoschem/geos-chem-cloud/issues/26. It talks about some general issues for AWS CLI on Windows. You might need to install "aws" via Conda, that might get rid of the "aws: command not found" error.

I have not used the AWS CLI interface because we use a shared account (for billing purposes) here and it is problematic to set up credentials securely when one or more people are logging in. So I just start up AWS instances by going directly to the EC2 console.