如何结合boto和fabric
How to combine boto with fabric
我需要提一下。我用 Windows.
现在我知道如何使用boto了。但是我遇到了不能基于boto 运行 "sudo" 的问题。
status, stdout, stderr = ssh_client.run('sudo python killerparser.py')
错误是 sudo: 对不起,你必须有一个 tty 到 运行 sudo
然后我尝试运行它。
status, stdout, stderr = ssh_client.run('ssh -t localhost sudo python killerparser.py')
但是现在错误变成了'Pseudo-terminal will not be allocated because stdin is not a terminal.\r\nHost key verification failed.'
我不想更改不安全的用户数据。所以想到了使用织物的想法。但是如何定义主机和密钥路径呢?我认为 fabric 不是基于对象的,这真的很令人沮丧。
我的所有代码:
import boto.ec2
from boto.manage.cmdshell import sshclient_from_instance
from fabric.api import env, run, cd, settings, sudo,hosts;
env.host = 'ec2-user@#.#.#.#'
env.user = "ec2-user"
env.key_filename = "D:\key.pem"
conn = boto.ec2.connect_to_region('us-east-1',aws_access_key_id="***",aws_secret_access_key="*")
instance = conn.get_all_instances(['***'])[0].instances[0]
ssh_client = sshclient_from_instance(instance,
ssh_key_file='**',
user_name='ec2-user')
sudo("cd ~");
sudo("python killerparser.py");
现在没有错误了。但是它不能执行 shell
killerparser.py
import subprocess, signal,os;
for line in os.popen("ps ax | grep -i newLive.py"):
if "grep" in line: continue;
fields = line.split()
pid = fields[0]
os.kill(int(pid), signal.SIGKILL)
proc = subprocess.Popen('sudo python newLive.py 2>newLive.err', shell=True,
stdin=None, stdout=None, stderr=None, close_fds=True)
我不同意你正在做的两件事。一:sudo python...
否。将 运行 设为 www-data 或等效数据。另外,请使用 supervisord
而不是您当前正在做的事情。
你是否在 windows 上应该无关紧要..你告诉我这对你不起作用?
fabfile.py:
import boto.ec2
from fabric.api import env, run, sudo, task
env.key_filename = "/PATH/TO/SSH/FILE.pem"
env.user = "ubuntu"
@task
def amazon(**kwargs):
conn = boto.ec2.connect_to_region(
'us-east-1',
aws_access_key_id="*********",
aws_secret_access_key="**************"
)
hosts = []
for reservation in conn.get_all_instances():
for instance in reservation.instances:
# if filters were applied
if kwargs:
skip_instance = False
for key, value in kwargs.items():
instance_value = getattr(instance, key)
# makes sure that `group` is handeled
if isinstance(instance_value, list):
new_value = []
for item in instance_value:
if isinstance(item, boto.ec2.group.Group):
new_value.append(item.name)
else:
new_value.append(item)
instance_value = new_value
if value not in instance_value:
skip_instance = True
break
else:
# every other single value gets handeled here
if instance_value != value:
skip_instance = True
break
if skip_instance:
continue
if instance.dns_name:
hosts.append(instance.dns_name)
elif instance.ip_address:
hosts.append(instance.ip_address)
env.hosts = hosts
@task
def whoami():
run('whoami')
sudo('whoami')
我为您添加了过滤器,以防万一,您可以运行将其设为:
fab amazon whoami
-- 它将遍历亚马逊中的每一台服务器并连接和 运行 whoami
命令。
fab amazon:ip_address=<IP OF AN INSTANCE YOU KNOW OF> whoami
-- 将只使用在过滤器上匹配 ip 的框。 (它应该适用于 boto 中 instance
中的每个字段)
那个只是个噱头,groups
是"I"会用的:
fab amazon:groups=<GROUP NAME FROM AMAZON> whoami
-- 将 运行 whoami
在与所述组名匹配的所有服务器上。
证明:
$ fab amazon:dns_name=******* whoami
[*******] Executing task 'whoami'
[*******] run: whoami
[*******] out: ubuntu
[*******] out:
[*******] sudo: whoami
[*******] out: root
[*******] out:
Done.
Disconnecting from *******... done.
和
$ fab amazon:groups=webservers whoami
[***1***] Executing task 'whoami'
[***1***] run: whoami
[***1***] out: ubuntu
[***1***] out:
[***1***] sudo: whoami
[***1***] out: root
[***1***] out:
... truncated...
[***4***] Executing task 'whoami'
[***4***] run: whoami
[***4***] out: ubuntu
[***4***] out:
[***4***] sudo: whoami
[***4***] out: root
[***4***] out:
Done.
Disconnecting from ***1***... done.
Disconnecting from ***2***... done.
Disconnecting from ***3***... done.
Disconnecting from ***4***... done.
Disconnecting from ***5***... done.
Disconnecting from ***6***... done.
Disconnecting from ***7***... done.
我需要提一下。我用 Windows.
现在我知道如何使用boto了。但是我遇到了不能基于boto 运行 "sudo" 的问题。
status, stdout, stderr = ssh_client.run('sudo python killerparser.py')
错误是 sudo: 对不起,你必须有一个 tty 到 运行 sudo
然后我尝试运行它。
status, stdout, stderr = ssh_client.run('ssh -t localhost sudo python killerparser.py')
但是现在错误变成了'Pseudo-terminal will not be allocated because stdin is not a terminal.\r\nHost key verification failed.'
我不想更改不安全的用户数据。所以想到了使用织物的想法。但是如何定义主机和密钥路径呢?我认为 fabric 不是基于对象的,这真的很令人沮丧。 我的所有代码:
import boto.ec2
from boto.manage.cmdshell import sshclient_from_instance
from fabric.api import env, run, cd, settings, sudo,hosts;
env.host = 'ec2-user@#.#.#.#'
env.user = "ec2-user"
env.key_filename = "D:\key.pem"
conn = boto.ec2.connect_to_region('us-east-1',aws_access_key_id="***",aws_secret_access_key="*")
instance = conn.get_all_instances(['***'])[0].instances[0]
ssh_client = sshclient_from_instance(instance,
ssh_key_file='**',
user_name='ec2-user')
sudo("cd ~");
sudo("python killerparser.py");
现在没有错误了。但是它不能执行 shell
killerparser.py
import subprocess, signal,os;
for line in os.popen("ps ax | grep -i newLive.py"):
if "grep" in line: continue;
fields = line.split()
pid = fields[0]
os.kill(int(pid), signal.SIGKILL)
proc = subprocess.Popen('sudo python newLive.py 2>newLive.err', shell=True,
stdin=None, stdout=None, stderr=None, close_fds=True)
我不同意你正在做的两件事。一:sudo python...
否。将 运行 设为 www-data 或等效数据。另外,请使用 supervisord
而不是您当前正在做的事情。
你是否在 windows 上应该无关紧要..你告诉我这对你不起作用?
fabfile.py:
import boto.ec2
from fabric.api import env, run, sudo, task
env.key_filename = "/PATH/TO/SSH/FILE.pem"
env.user = "ubuntu"
@task
def amazon(**kwargs):
conn = boto.ec2.connect_to_region(
'us-east-1',
aws_access_key_id="*********",
aws_secret_access_key="**************"
)
hosts = []
for reservation in conn.get_all_instances():
for instance in reservation.instances:
# if filters were applied
if kwargs:
skip_instance = False
for key, value in kwargs.items():
instance_value = getattr(instance, key)
# makes sure that `group` is handeled
if isinstance(instance_value, list):
new_value = []
for item in instance_value:
if isinstance(item, boto.ec2.group.Group):
new_value.append(item.name)
else:
new_value.append(item)
instance_value = new_value
if value not in instance_value:
skip_instance = True
break
else:
# every other single value gets handeled here
if instance_value != value:
skip_instance = True
break
if skip_instance:
continue
if instance.dns_name:
hosts.append(instance.dns_name)
elif instance.ip_address:
hosts.append(instance.ip_address)
env.hosts = hosts
@task
def whoami():
run('whoami')
sudo('whoami')
我为您添加了过滤器,以防万一,您可以运行将其设为:
fab amazon whoami
-- 它将遍历亚马逊中的每一台服务器并连接和 运行 whoami
命令。
fab amazon:ip_address=<IP OF AN INSTANCE YOU KNOW OF> whoami
-- 将只使用在过滤器上匹配 ip 的框。 (它应该适用于 boto 中 instance
中的每个字段)
那个只是个噱头,groups
是"I"会用的:
fab amazon:groups=<GROUP NAME FROM AMAZON> whoami
-- 将 运行 whoami
在与所述组名匹配的所有服务器上。
证明:
$ fab amazon:dns_name=******* whoami
[*******] Executing task 'whoami'
[*******] run: whoami
[*******] out: ubuntu
[*******] out:
[*******] sudo: whoami
[*******] out: root
[*******] out:
Done.
Disconnecting from *******... done.
和
$ fab amazon:groups=webservers whoami
[***1***] Executing task 'whoami'
[***1***] run: whoami
[***1***] out: ubuntu
[***1***] out:
[***1***] sudo: whoami
[***1***] out: root
[***1***] out:
... truncated...
[***4***] Executing task 'whoami'
[***4***] run: whoami
[***4***] out: ubuntu
[***4***] out:
[***4***] sudo: whoami
[***4***] out: root
[***4***] out:
Done.
Disconnecting from ***1***... done.
Disconnecting from ***2***... done.
Disconnecting from ***3***... done.
Disconnecting from ***4***... done.
Disconnecting from ***5***... done.
Disconnecting from ***6***... done.
Disconnecting from ***7***... done.