Skip to content

Commit 668e70f

Browse files
committed
add fabric 2 sample
1 parent fea1e4e commit 668e70f

7 files changed

+93
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,5 @@ ENV/
9191

9292
# Rope project settings
9393
.ropeproject
94+
*/.pytest_cache/
95+
.vscode

fabric2-sample/README.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# fabric2-sample
2+
3+
use fabric v2 to execute shell commands remotely over SSH.
4+
5+
[Tutorial](https://docs.fabfile.org/en/2.5/getting-started.html#addendum-the-fab-command-line-tool)
6+
7+
## Running on host
8+
9+
```bash
10+
# create virtual env
11+
$ virtualenv venv -p python3 && source venv/bin/activate
12+
$ pip install --upgrade pip && \
13+
pip install -r requirements.txt && \
14+
pip install -r requirements-dev.txt
15+
```
16+
17+
[Command-line interface](https://docs.fabfile.org/en/2.5/cli.html)
18+
19+
```bash
20+
# running command
21+
$ fab --list
22+
$ fab -H dev.gpu install-zsh
23+
```
24+
25+
[Configuration](https://docs.fabfile.org/en/2.5/concepts/configuration.html) ./fabric.yaml
26+
27+
```yaml
28+
connect_kwargs:
29+
password: welcome-to-heaven
30+
sudo:
31+
password: welcome-to-heaven
32+
timeouts:
33+
connect: 10
34+
user: god
35+
port: 22
36+
```

fabric2-sample/fabfile.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from fabric import Connection, task
2+
3+
4+
@task
5+
def install_zsh(c):
6+
"""install oh-my-zsh
7+
8+
c [context](http://docs.pyinvoke.org/en/latest/api/context.html)
9+
10+
https://github.com/ohmyzsh/ohmyzsh
11+
"""
12+
c.run("echo $SHELL")
13+
# http://docs.pyinvoke.org/en/latest/api/runners.html
14+
if c.run("test -d ~/.oh-my-zsh", warn=True).failed:
15+
print("install zsh")
16+
c.sudo("apt install zsh -y")
17+
print("install oh-my-zsh")
18+
# c.run('sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"')
19+
c.put("install.sh", "/tmp/install.sh")
20+
c.run("sh /tmp/install.sh")
21+
# c.sudo("chsh -s $(which zsh)", warn=True)
22+
if c.run("test -f ~/.zshrc", warn=True).ok:
23+
print(f"install on {c.config['user']}@{c.host}")
24+
25+
# c.run("source ~/.zshrc", shell="/bin/zsh", env={})
26+
# c.run("omz theme use ys")
27+
# c.run("omz update")

fabric2-sample/requirements-dev.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
--index https://mirrors.aliyun.com/pypi/simple
2+
black>=20.8b1
3+
isort>=5.6.0
4+
mypy==0.790
5+
pytest>=6.1.0

fabric2-sample/requirements.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--index https://mirrors.aliyun.com/pypi/simple
2+
fabric==2.5.0
3+
python-dotenv>=0.15.0

fabric2-sample/ssh_config

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# https://docs.fabfile.org/en/2.5/concepts/configuration.html#ssh-config
2+
Host *
3+
ConnectTimeout 10
4+
ForwardAgent no
5+
ForwardX11 no
6+
ForwardX11Trusted yes
7+
User tx-deepocean
8+
Port 22
9+
Protocol 2
10+
connect_kwargs tx-deepocean tuixiang2017

fabric2-sample/util_cli.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# https://docs.python.org/zh-cn/3/library/subprocess.html
2+
import logging
3+
import subprocess
4+
5+
6+
def run(cmd, verbose=True) -> None:
7+
"""Run Command, use stdout as output"""
8+
if verbose:
9+
logging.debug(cmd)
10+
subprocess.run(args=cmd, shell=True, check=True)

0 commit comments

Comments
 (0)