in fabric 2/invoke: change directory and use sudo

  • Last Update :
  • Techknowledgy :

As the commands can grow quite big and to follow DRY principles, I created a function to do wrap this:

def sudo_cd(context, path, command):
   ""
"Workaround on the problem of cd not working with sudo command"
""
context.sudo(f 'bash -c "cd {path} && {command}"')

Suggestion : 2

While the SFTP protocol (which get uses) has no direct ability to download files from locations not owned by the connecting user, you may specify use_sudo=True to work around this. When set, this setting allows get to copy (using sudo) the remote files to a temporary location on the remote end (defaults to remote user’s $HOME; this may be overridden via temp_dir), and then download them to local_path.,While the SFTP protocol (which put uses) has no direct ability to upload files to locations not owned by the connecting user, you may specify use_sudo=True to work around this. When set, this setting causes put to upload the local files to a temporary location on the remote end (defaults to remote user’s $HOME; this may be overridden via temp_dir), and then use sudo to move them to remote_path.,In this case, put will attempt to read the entire contents of the file-like object by rewinding it using seek (and will use tell afterwards to preserve the previous file position).,Any text entered in your local terminal will be forwarded to the remote program as it runs, thus allowing you to interact with password or other prompts naturally. For more on how this works, see Interaction with remote programs.

# Simplest form:
   environment = prompt('Please specify target environment: ')

# With
default, and storing as env.dish:
   prompt('Specify favorite dish: ', 'dish',
      default = 'spam & eggs')

# With validation, i.e.requiring integer input:
   prompt('Please specify process nice level: ', key = 'nice', validate = int)

# With validation against a regular expression:
   release = prompt('Please supply a release name',
      validate = r '^\w+-\d+(\.\d+)?$')

# Prompt regardless of the global abort - on - prompts setting:
   with settings(abort_on_prompts = False):
   prompt('I seriously need an answer on this! ')
with cd('/tmp'):
   put('/path/to/local/test.txt', 'files')
put('bin/project.zip', '/tmp/project.zip')
put('*.py', 'cgi-bin/')
put('index.html', 'index.html', mode = 0755)
"Th(is|ese) variable(s) (are|is) used for %s"
run("ls /var/www/")
run("ls /home/myuser", shell = False)
output = run('ls /var/www/site1')
run("take_a_long_time", timeout = 5)
sudo("~/install_script.py")
sudo("mkdir /var/www/new_docroot", user = "www-data")
sudo("ls /home/jdoe", user = 1001)
result = sudo("ls /tmp/")
with settings(sudo_user = 'mysql'):
   sudo("whoami") # prints 'mysql'

Suggestion : 3

Set a default command execution timeout of INT seconds. Maps to the timeouts.command config setting.,Change root directory used for finding task modules.,Use a pty when executing shell commands.,All core options & flags are below; almost all of them must be given before any task names, with a few (such as --help) being specially looked for anywhere in the command line. (For parsing details, see Basic command line layout.)

$ inv[oke][--core - opts] task1[--task1 - opts]...taskN[--taskN - opts]
# Empty input: just task names
$ inv--complete--
foo
bar

# Input not ending with a dash: task names still
$ inv--complete--foo--foo - arg
foo
bar

# Input ending with a dash: current context 's flag names
$ inv--complete--foo -
   --foo - arg
   --foo - arg - 2
$ invoke--print - completion - script zsh
$ fab--print - completion - script bash
$ source < (inv--print - completion - script zsh)
$ inv--print - completion - script zsh > ~/.invoke-completion.sh

Suggestion : 4

You can specify a custom directory to use as temporary storage for executable files that you do not want to have stored in the temp dir directory. Provide an environment variable for the directory that is exported when the plugin runs.,The second example demonstrates that you can still use ctx to execute commands as if you are running it from a bash script.,Using a tasks file instead of a list of commands enables you to use python code to execute commands. In addition, you will be able to use the ctx object to perform actions based on contextual data.,The most notable difference is that, to get all properties for a node or runtime properties for a node instance, you have to run the following:

imports:
   -http: //www.getcloudify.org/spec/fabric-plugin/1.3/plugin.yaml

   node_templates:
   example_node:
   type: cloudify.nodes.WebServer
interfaces:
   cloudify.interfaces.lifecycle:
   start:
   implementation: fabric.fabric_plugin.tasks.run_commands
inputs:
   commands:
   -echo "source ~/myfile" >> ~/.bashrc -
   apt - get install - y python - dev git -
   pip install my_module
imports:
   -http: //www.getcloudify.org/spec/fabric-plugin/1.3/plugin.yaml

   node_templates:
   example_node:
   type: cloudify.nodes.WebServer
interfaces:
   cloudify.interfaces.lifecycle:
   start:
   implementation: fabric.fabric_plugin.tasks.run_task
inputs:
   tasks_file: my_tasks / tasks.py
task_name: install_nginx
task_properties:
   important_prop1: very_important
important_prop2: 300
#my_tasks / tasks.py
from fabric.api
import run, put
from cloudify
import ctx

def install_nginx(important_prop1, important_prop2):
   ctx.logger.info('Installing nginx. Some important props:'
      ' prop1: {0}, prop2: {1}'
      .format(important_prop1, important_prop2))
run('sudo apt-get install nginx')

def configure_nginx(config_file_path):
   # configure the webserver to run with our premade configuration file.
conf_file = ctx.download_resource(config_file_path)
put(conf_file, '/etc/nginx/conf.d/')

def start_nginx(ctx):
   run('sudo service nginx restart')
#my_tasks / tasks.py
from fabric2
import task
from cloudify
import ctx

@task
def install_nginx(connection, important_prop1, important_prop2):
   ctx.logger.info('Installing nginx. Some important props:'
      ' prop1: {0}, prop2: {1}'
      .format(important_prop1, important_prop2))
connection.run('sudo apt-get install nginx')

@task
def configure_nginx(connection, config_file_path):
   # configure the webserver to run with our premade configuration file.
conf_file = ctx.download_resource(config_file_path)
connection.put(conf_file, '/etc/nginx/conf.d/')

@task
def start_nginx(connection, ctx):
   connection.run('sudo service nginx restart')
imports:
   -http: //www.getcloudify.org/spec/fabric-plugin/1.3/plugin.yaml

   node_templates:
   example_node:
   type: cloudify.nodes.WebServer
interfaces:
   cloudify.interfaces.lifecycle:
   start:
   implementation: fabric.fabric_plugin.tasks.run_module_task
inputs:
   task_mapping: some_package.some_module.install_nginx
task_properties:
   important_prop1: very_important
important_prop2: 300
node_templates:
   example_node:
   type: cloudify.nodes.WebServer
interfaces:
   cloudify.interfaces.lifecycle:
   start:
   implementation: fabric.fabric_plugin.tasks.run_script
inputs:
   # Path to the script relative to the blueprint directory
script_path: scripts / start.sh
MY_ENV_VAR: some - value

Suggestion : 5

After you have used the network.sh to create a channel, you can start a chaincode on the channel using the following command:,You can find the scripts to bring up the network in the test-network directory of the fabric-samples repository. Navigate to the test network directory by using the following command:,From inside the test-network directory, run the following command to remove any containers or artifacts from any previous runs:,Chaincodes are invoked when a network member wants to transfer or change an asset on the ledger. Use the following command to change the owner of an asset on the ledger by invoking the asset-transfer (basic) chaincode:

curl - sSL https: //bit.ly/2ysbOFE | bash -s -- 2.2.2 1.4.9
cd fabric - samples / test - network
. / network.sh down
. / network.sh up
Creating network "fabric_test"
with the
default driver
Creating volume "net_orderer.example.com"
with
default driver
Creating volume "net_peer0.org1.example.com"
with
default driver
Creating volume "net_peer0.org2.example.com"
with
default driver
Creating peer0.org2.example.com...done
Creating orderer.example.com...done
Creating peer0.org1.example.com...done
Creating cli...done
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1667543 b5634 hyperledger / fabric - tools: latest "/bin/bash"
1 second ago Up Less than a second cli
b6b117c81c7f hyperledger / fabric - peer: latest "peer node start"
2 seconds ago Up 1 second 0.0 .0 .0: 7051 - > 7051 / tcp peer0.org1.example.com
703 ead770e05 hyperledger / fabric - orderer: latest "orderer"
2 seconds ago Up Less than a second 0.0 .0 .0: 7050 - > 7050 / tcp, 0.0 .0 .0: 7053 - > 7053 / tcp orderer.example.com
718 d43f5f312 hyperledger / fabric - peer: latest "peer node start"
2 seconds ago Up 1 second 7051 / tcp, 0.0 .0 .0: 9051 - > 9051 / tcp peer0.org2.example.com