Removing MyPLC credentials requirements in update_fedora_repo.py
[nepi.git] / examples / planetlab / update_fedora_repo.py
1 #!/usr/bin/env python
2 #
3 #    NEPI, a framework to manage network experiments
4 #    Copyright (C) 2013 INRIA
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU General Public License as published by
8 #    the Free Software Foundation, either version 3 of the License, or
9 #    (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU General Public License for more details.
15 #
16 #    You should have received a copy of the GNU General Public License
17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
20
21 # THIS IS A MAINTAINANCE SCRIPT!
22 # Use this script only if you are getting the following error when using yum:
23 #
24 #   Error: Cannot retrieve repository metadata (repomd.xml) for repository: fedora. Please verify its path and try again
25
26 # Example of how to run this experiment (replace with your information):
27 #
28 # $ cd <path-to-nepi>
29 # python examples/planetlab/update_fedora_repo.py -H <host1,host2,..> -s <pl-slice> -u <pl-user> -p <pl-password> -k <pl-ssh-key>  
30
31 from nepi.execution.ec import ExperimentController 
32
33 from optparse import OptionParser, SUPPRESS_HELP
34 import os
35
36 pl_slice = os.environ.get("PL_SLICE")
37 pl_user = os.environ.get("PL_USER")
38 pl_pass = os.environ.get("PL_PASS")
39 identity = os.environ.get("PL_SSHKEY")
40
41 usage = ("usage: %prog -H <hosts> -s <pl-slice> -u <pl-user> -p <pl-password> "
42         "-k <pl-ssh-key>")
43
44 parser = OptionParser(usage = usage)
45 parser.add_option("-H", "--hosts", dest="hosts",
46         help="Comma separated list of PlanetLab host to update", type="str")
47 parser.add_option("-s", "--pl-slice", dest="pl_slice",
48         help="PlanetLab slicename", type="str", default=pl_slice)
49 parser.add_option("-u", "--pl-user", dest="pl_user",
50         help="PlanetLab web username", type="str", default=pl_user)
51 parser.add_option("-p", "--pl-password", dest="pl_password",
52         help="PlanetLab web password", type="str", default=pl_pass)
53 parser.add_option("-k", "--pl-ssh-key", dest="pl_ssh_key",
54         help="Path to private SSH key associated with the PL account",
55         type="str", default=identity)
56
57 (options, args) = parser.parse_args()
58
59 proceed = raw_input ("Executing this script will modify the fedora yum repositories in the selected PlanetLab hosts. Are you sure to continue? [y/N] ")
60 if proceed.lower() not in ['yes', 'y']:
61     os._exit(1)
62
63 pl_slice = options.pl_slice
64 pl_ssh_key = options.pl_ssh_key
65 pl_user = options.pl_user
66 pl_password = options.pl_password
67 hosts = options.hosts
68
69 hosts = map(str.strip, hosts.split(","))
70 apps = []
71
72 ## Create the experiment controller
73 ec = ExperimentController(exp_id = "pl_update_fedora_repo")
74
75 for hostname in hosts:
76     # Register a Planetlab Node with no restrictions, it can be any node
77     node = ec.register_resource("planetlab::Node")
78     # The username in this case is the slice name, the one to use for login in 
79     # via ssh into PlanetLab nodes. Replace with your own slice name.
80     if hostname != "any":
81         ec.set(node, "hostname", hostname)
82
83     ec.set(node, "username", pl_slice)
84     ec.set(node, "identity", pl_ssh_key)
85     # The pluser and plpassword are the ones used to login in the PlanetLab web 
86     # site. Replace with your own user and password account information.
87     if pl_user:
88         ec.set(node, "pluser", pl_user)
89     if pl_password: 
90         ec.set(node, "plpassword", pl_password)
91
92     ec.set(node, "critical", False)
93     # Remove previous results
94     ec.set(node, "cleanExperiment", True)
95     ec.set(node, "cleanProcesses", True)
96
97     # Application to update fedora.repo
98     path_to_repo = os.path.join(
99             os.path.dirname(os.path.realpath(__file__)),
100             "fedora.repo")
101
102     app = ec.register_resource("linux::Application")
103     ec.set(app, "files", path_to_repo)
104     ec.set(app, "sudo", True) 
105     ec.set(app, "command", 
106         "cp /etc/yum.repos.d/fedora.repo /etc/yum.repos.d/fedora.repo.old; "
107         "cp ${SHARE}/fedora.repo /etc/yum.repos.d/fedora.repo")
108     ec.set(app, "critical", False)
109     ec.register_connection(node, app)
110
111     apps.append(app)
112
113     # Application to update fedora.repo
114     path_to_repo = os.path.join(
115             os.path.dirname(os.path.realpath(__file__)),
116             "fedora-updates.repo")
117
118     app = ec.register_resource("linux::Application")
119     ec.set(app, "files", path_to_repo)
120     ec.set(app, "sudo", True) 
121     ec.set(app, "command", 
122         "cp /etc/yum.repos.d/fedora-updates.repo /etc/yum.repos.d/fedora-updates.repo.old; "
123         "cp ${SHARE}/fedora-updates.repo /etc/yum.repos.d/fedora-updates.repo")
124     ec.set(app, "critical", False)
125     ec.register_connection(node, app)
126     
127     apps.append(app)
128
129
130 ec.deploy()
131
132 ec.wait_finished(apps)
133
134 for app in apps:
135     try:
136         print ec.trace(app, "stderr")
137     except:
138         print "NO stderr"
139
140 ec.shutdown()
141