YONGFEIUALL

izheyi.com


  • Home

  • Archives

  • Categories

  • Tags

  • About

  • Search

Python read remote file from linux server

Posted on 2017-12-07 | In Python |

自动化要读取Server上的文件来判断Json的正确与否,还是通过Paramiko实现,这里一个简单例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if __name__ == "__main__":
hostname = 'xxx'
port = 22
username = 'xxx'
password = 'xxx'

s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname, port, username, password)

sftp_client = s.open_sftp()
remote_file = sftp_client.open('/tmp/file')
try:
for line in remote_file:
print line
finally:
remote_file.close()
s.close()

Docker - Dockerfile Customize Image

Posted on 2017-12-02 | In Docker |

镜像的定制实际上就是定制每一层所添加的配置、文件。Dockerfile是一个文本文件,其内包含了一条条的指令,每一条指令构建一层,因此每一条指令的内容,就是描述该层应当如何构建。

基本命令

例子:

1
2
FROM nginx
RUN echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html

这里只介绍两个最基本的,还有很多,用到的时候再学习。

From

指定基础image,在其上进行定制,必须要有且是第一条指令。

Run

是用来执行命令行命令的。主要有两种格式:

  • Shell
    RUN <命令>,就像直接在命令行中输入的命令一样。
  • Exec
    RUN ["可执行文件", "参数1", "参数2"],这更像是函数调用中的格式。

Union FS 是有最大层数限制的,所以别用太多Run命令建很多层,如果有多条命令要执行,使用 && 将各个所需命令串联起来。
Dockerfile 支持 Shell 类的行尾添加 \ 的命令换行方式,以及行首 # 进行注释的格式。

到此,要定制的内容就完成了,下面就是构建了。

构建

用 docker build 命令来构建镜像。

例子:

1
2
3
4
5
6
7
8
9
10
docker@default:~/myngix$ docker build -t nginx:v5 .
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM nginx
---> 9e7424e5dbae
Step 2/2 : RUN echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html
---> Running in 0f77e86fba64
---> d725428dfdad
Removing intermediate container 0f77e86fba64
Successfully built d725428dfdad
Successfully tagged nginx:v5

1
2
3
4
docker@default:~/myngix$ docker image ls -a
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx v5 d725428dfdad 16 seconds ago 108MB
nginx latest 9e7424e5dbae 2 weeks ago 108MB

Docker-Container

Posted on 2017-11-28 | In Docker |

Docker运行容器前需要本地存在对应的镜像,如果镜像不存在本地,Docker会从镜像仓库下载(默认是Docker Hub)。
当利用docker run来创建容器时,Docker在后台运行的标准操作包括:

  • 检查本地是否存在指定的镜像,不存在就从公有仓库下载
  • 利用镜像创建并启动一个容器
  • 分配一个文件系统,并在只读的镜像层外面挂载一层可读写层
  • 从宿主主机配置的网桥接口中桥接一个虚拟接口到容器中去
  • 从地址池配置一个 ip 地址给容器
  • 执行用户指定的应用程序
  • 执行完毕后容器被终止

启动容器

启动容器有两种方式,一种是基于镜像新建一个容器并启动,另外一个是将在终止状态(stopped)的容器重新启动。

新建启动

所用的命令 docker run。

1
2
docker@default:~$ docker container run centos /bin/echo 'hello world'
hello world

经常用的Options:-it –rm

1
2
3
docker@default:~$ docker run -it centos /bin/bash
[root@6d1e9498e298 /]# pwd
/

需要让Docker在后台运行而不是直接把执行命令的结果输出在当前宿主机下,可以通过添加-d参数来实现。

终止的容器启动

直接将一个已经终止的容器启动运行, 命令docker container start。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
docker@default:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6d1e9498e298 centos "/bin/bash" 2 minutes ago Exited (130) 5 seconds ago hardcore_mclean
docker@default:~$ docker container start --help

Usage: docker container start [OPTIONS] CONTAINER [CONTAINER...]

Start one or more stopped containers

Options:
-a, --attach Attach STDOUT/STDERR and forward signals
--detach-keys string Override the key sequence for detaching a container
--help Print usage
-i, --interactive Attach container's STDIN
docker@default:~$ docker start 6d1e9498e298
6d1e9498e298
docker@default:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6d1e9498e298 centos "/bin/bash" 5 minutes ago Up 11 seconds hardcore_mclean

此外,docker restart命令会将一个运行态的容器终止,然后再重新启动它。

Read more »

Docker - Image

Posted on 2017-11-19 | In Docker |

镜像是多层存储结构,并且可以继承、复用,因此不同镜像可能会因为使用相同的基础镜像,从而拥有共同的层。Docker使用Union FS,相同的层只需要保存一份即可。

镜像层数量可能会很多,所有镜像层会联合在一起组成一个统一的文件系统。

新镜像是从base镜像一层一层叠加生成的。每安装一个软件,就在现有镜像的基础上增加一层。

获取镜像

可以到Docker Hub去查找镜像。

从Docker Registry获取镜像的命令是docker image pull。其命令格式为:

1
2
3
4
5
6
7
8
9
10
docker@default:~$ docker image pull --help

Usage: docker image pull [OPTIONS] NAME[:TAG|@DIGEST]

Pull an image or a repository from a registry

Options:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
--help Print usage

默认地址是 Docker Hub。如果不用默认,地址的格式一般是 <域名/IP>[:端口号]。

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
docker@default:~$ git image pull mongo
Using default tag: latest
latest: Pulling from library/mongo
d13d02fa248d: Pull complete
bc8e2652ce92: Pull complete
3cc856886986: Pull complete
c319e9ec4517: Pull complete
b4cbf8808f94: Pull complete
cb98a53e6676: Pull complete
f0485050cd8a: Pull complete
ac36cdc414b3: Pull complete
61814e3c487b: Pull complete
523a9f1da6b9: Pull complete
3b4beaef77a2: Pull complete
Digest: sha256:d13c897516e497e898c229e2467f4953314b63e48d4990d3215d876ef9d1fc7c
Status: Downloaded newer image for mongo:latest

查看本地镜像

想查看已经下载的镜像, 格式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
docker@default:~$ docker image ls --help

Usage: docker image ls [OPTIONS] [REPOSITORY[:TAG]]

List images

Options:
-a, --all Show all images (default hides intermediate images)
--digests Show digests
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print images using a Go template
--help Print usage
--no-trunc Don't truncate output
-q, --quiet Only show numeric IDs

不只可以列出所有的镜像,还可以列出特定的某个镜像。

例子:

1
2
3
4
docker@default:~$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
mongo latest d22888af0ce0 3 weeks ago 361MB
hello-world latest 725dcfab7d63 3 weeks ago 1.84kB

Read more »

Docker - 基础和安装

Posted on 2017-11-18 | In Docker |

Docker现在火的不得了,也一直很好奇,利用一些时间简单的学习一下,知道一下到底是个什么东西,基本的原理和应用。

初学,只是简单的入门和学习的一个记录,如有不对,欢迎指正。

Docker介绍

Docker 是一个开源的应用容器引擎,可以轻松的为任何应用创建一个轻量级的、可移植的、自给自足的容器。容器是完全使用沙箱机制,相互之间不会有任何接口,更重要的是容器性能开销极低。
通俗讲docker就是集装箱原理,从图标就可以看出来^_^

  • Docker Client: Docker Client提供给用户一个终端,用户输入Docker提供的命令来管理本地或者远程的服务器。
  • Docker Host: 一个物理或者虚拟的机器用于执行Docker守护进程和容器。
  • Docker Daemon: 每台服务器上只要安装了Docker的环境,基本上就跑了一个后台程序Docker Daemon,Docker Daemon会接收Docker Client发过来的指令,并对服务器的进行具体操作。
  • Docker Images: 用于创建Docker容器的模板。是只读的。
  • Docker Container: 是真正跑项目程序、消耗机器资源、提供服务的地方,Docker Container通过Docker Images启动,在Docker Images的基础上运行你需要的代码。
  • Docker Registry: 是管理仓库的具体服务器,每个服务器上可以有多个仓库,而每个仓库下面有多个镜像。
  • Docker Machine: 是一个简化Docker安装的命令行工具,通过一个简单的命令行即可在相应的平台上安装Docker并管理Host。

Docker安装

试验环境是Win7

从Docker官网下载:Docker-Windows,安装方式,Windows下就是双击,下一步,下一步了。。。

Docker运行

安装完毕后,在应用程序里点击‘Docker Quickstart Terminal’打开Docker,但是有下边这个错误提示: “Looks like something went wrong in step ‘Finalize’…”
Solution:
在‘docker toolbox’的安装目录下,打开‘start.sh’,做如下修改(注释掉clear行):

1
2
3
STEP="Finalize"
#clear
cat << EOF

运行结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
                        ##         .
## ## ## ==
## ## ## ## ## ===
/"""""""""""""""""\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\_______/

docker is configured to use the default machine with IP 192.168.99.100
For help getting started, check out the docs at https://docs.docker.com

Start interactive shell

yongfei.hu@yongfei-hu MINGW64 ~
$ docker version
Client:
Version: 17.10.0-ce
API version: 1.33
Go version: go1.8.3
Git commit: f4ffd25
Built: Tue Oct 17 19:00:02 2017
OS/Arch: windows/amd64

Server:
Version: 17.10.0-ce
API version: 1.33 (minimum version 1.12)
Go version: go1.8.3
Git commit: f4ffd25
Built: Tue Oct 17 19:05:23 2017
OS/Arch: linux/amd64
Experimental: false

Docker Machine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ docker-machine ssh default
## .
## ## ## ==
## ## ## ## ## ===
/"""""""""""""""""\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\_______/
_ _ ____ _ _
| |__ ___ ___ | |_|___ \ __| | ___ ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| < __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 17.10.0-ce, build HEAD : 34fe485 - Wed Oct 18 17:16:34 UTC 2017
Docker version 17.10.0-ce, build f4ffd25
docker@default:~$

Docker生命周期

Docker三个基本概念

  • 镜像(Image)
  • 容器(Container)
  • 仓库(Repository)

理解了这三个概念,就理解了整个生命周期。我们可以把镜像,容器想像成java的类和对象,即容器是由镜像实例化而来的。

奇点绘画画随记(2018)

Posted on 2017-11-14 | In 丁丁 |

天天吵着要报个课外班学习,如愿。看来姑娘是真的喜欢画画,每次都很期待上课。

2017





2018









Pytest run failed after renaming project folder

Posted on 2017-11-10 | In Python |

Pytest running failed after renaming a project folder, prompt could find file error.

Solution:

Delete the python cache (pycache) and all “compiled” files.

Make sure that your init.py files are not referring to any folder or path.

pytest - rerun failed script

Posted on 2017-11-06 | In Python |

都知道有很多的因素会造成自动化脚本Failed,最好的方式是能在Failed后再自动的跑一下,以达到更好的自动化的效用。

Google发现有现成的,就直接用:pytest-rerunfailures

并用做了验证,没毛病:

  1. 准备用例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #coding=utf-8

    class TestClass:

    def test_one(self):
    x = "this"
    assert "h0" in x

    def test_two(self):
    x = "hello"
    assert x == "hello"
  2. 验证Rerun

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    C:\Python27\Scripts>py.test -r aR --rerun 2 1\1.py
    ============================= test session starts =============================
    platform win32 -- Python 2.7.13, pytest-3.2.3, py-1.4.34, pluggy-0.4.0
    rootdir: C:\Python27\Scripts, inifile:
    plugins: rerunfailures-3.1
    collected 2 items

    1\1.py RRF.
    =========================== rerun test summary info ===========================
    RERUN 1/1.py::TestClass::()::test_one
    RERUN 1/1.py::TestClass::()::test_one
    =========================== short test summary info ===========================
    FAIL 1/1.py::TestClass::()::test_one

    ================================== FAILURES ===================================
    _____________________________ TestClass.test_one ______________________________

    self = <1.TestClass instance at 0x03EA3C10>

    def test_one(self):
    x = "this"
    > assert "h0" in x
    E AssertionError: assert 'h0' in 'this'

    1\1.py:7: AssertionError
    ================= 1 failed, 1 passed, 2 rerun in 0.11 seconds =================

Win7 安装curl

Posted on 2017-10-26 | In Curl |

Curl命令可以通过命令行的方式,执行Http请求。

介绍一下在Window 64位下的安装。

下载安装

从Curl,下载工具包,找到相应的版本。

使用

  1. 在解压目录下使用
    解压后目录’C:\curl-7.57.0\I386’

  2. 把exe放到System32
    拷贝I386/curl.exe文件到C:\Windows\System32, 就可以在DOS窗口中任意位置,使用curl命令了。

  3. 增加环境变量
    给path加环境变量,效果和第2种一次。

验证

1
2
3
4
5
6
7
8
9
10
C:\Users\youfei.hu>curl
curl: try 'curl --help' or 'curl --manual' for more information

C:\Users\youfei.hu>curl www.google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.com.hk/?gfe_rd=cr&amp;dcr=0&amp;ei=xoAnWqngJ6P48AeVl5SIAw">here</A>.
</BODY></HTML>

Python run command on romote linux

Posted on 2017-10-17 | In Python |

自动化需要,要远程执行Linux命令,使用了Paramiko

安装

非常简单

1
pip install paramiko

使用

1
2
3
4
5
6
7
8
9
10
11
12
import paramiko

def ssh_exec(hostname, port, username, password, execmd):

s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())

s.connect(hostname=hostname, port=port, username=username, password=password)
stdin, stdout, stderr = s.exec_command(execmd)
stdin.write("Y") # Generally speaking, the first connection, need a simple interaction.

s.close()

调用

1
2
3
4
5
6
7
8
if __name__ == "__main__":
hostname = 'xxx.com'
port = 22
username = 'xxx'
password = 'xxx'
execmd = "cd /home/qa/" # 执行多条命令用分号分隔

ssh_exec(hostname, port, username, password, execmd)

1…192021…40
唐胡璐

唐胡璐

i just wanna live while i am alive

393 posts
42 categories
74 tags
RSS
LinkedIn Weibo GitHub E-Mail
Creative Commons
© 2022 唐胡璐
Powered by Hexo
|
Theme — NexT.Pisces v5.1.4