#!/usr/bin/env python
import sys
import os
from P4 import P4, P4Exception
import sys
sys.path.append("/usr/local/lib/netapp-manageability-sdk-5.3.1/lib/python/NetApp")
from NaServer import *
if sys.version_info[0] >= 3:
from configparser import ConfigParser
else:
from ConfigParser import ConfigParser
## ---------------------------------------------------------------------------
## Singleton class (of sorts) for loading configuration
## ---------------------------------------------------------------------------
class Config:
def __init__(self, file):
self.config = ConfigParser()
try:
self.config.read(file)
except:
print("Could not read config file: %s" % file)
sys.exit(2)
def get(self, key):
parts = key.split(".")
try:
value = self.config.get(parts[0], parts[1])
except:
print("Could not access config: %s" % key)
sys.exit(2)
return value
dir = os.path.dirname(os.path.realpath(__file__))
config = Config(dir + "/flex.cfg")
FLEX_SNAP = config.get("p4.snap")
FLEX_CLONE = config.get("p4.clone")
## ---------------------------------------------------------------------------
## ---------------------------------------------------------------------------
## NetApp Server connection as 'admin' user
## ---------------------------------------------------------------------------
class NaFlex:
def __init__(self):
self.server = config.get("NaServer.server")
self.port = config.get("NaServer.port")
self.user = config.get("NaServer.admin_user")
self.passwd = config.get("NaServer.admin_passwd")
self.vserver = config.get("NaServer.vserver")
def get(self):
s = NaServer(self.server, 1 , 30)
s.set_server_type("FILER")
s.set_transport_type("HTTPS")
s.set_port(self.port)
s.set_style("LOGIN")
s.set_admin_user(self.user, self.passwd)
s.set_vserver(self.vserver)
return s
def create(self, name, size):
api = NaElement("volume-create")
api.child_add_string("containing-aggr-name","p4")
api.child_add_string("size", size)
api.child_add_string("volume", name)
xo = self.get().invoke_elem(api)
print (xo.sprintf())
if (xo.results_status() == "failed") :
print ("Error:\n")
print (xo.sprintf())
sys.exit(1)
## ---------------------------------------------------------------------------
## ---------------------------------------------------------------------------
## Perforce connection as 'flex' user
## ---------------------------------------------------------------------------
class P4Flex:
def __init__(self):
self.port = config.get("p4.port")
self.user = config.get("p4.user")
self.passwd = config.get("p4.passwd")
def getP4(self):
p4 = P4()
p4.user = self.user
p4.port = self.port
p4.connect()
p4.password = self.passwd
p4.run_login()
return p4
## ---------------------------------------------------------------------------
## ---------------------------------------------------------------------------
## Parse the Broker's request and provide access to the user's environment
## ---------------------------------------------------------------------------
class Broker:
def __init__(self):
self.args = {}
## Comment out stdin and uncomment block below for debug.
lines = sys.stdin.readlines()
#lines = []
#with open("in.txt") as f:
# lines = f.readlines()
for line in lines:
parts = line.split(": ")
self.args[parts[0]] = parts[1].rstrip()
def getP4(self):
p4 = P4()
p4.user = self.args['user']
p4.port = self.args['clientPort']
p4.client = self.args['workspace']
p4.connect()
return p4
def getUser(self):
return self.args['user']
def getClient(self):
return self.args['workspace']
def getCommand(self):
if 'Arg0' in self.args:
return self.args['Arg0']
else:
return None
def getOptions(self):
c = 1
opts = []
while True:
key = 'Arg' + str(c)
c += 1
if key in self.args:
opts.append(self.args[key])
else:
break
join = ""
list = []
for opt in opts:
if join:
opt = join + opt
join = ""
if opt.startswith('-') and len(opt) == 2:
join = opt
else:
list.append(opt)
if join:
list.append(opt)
return list
## ---------------------------------------------------------------------------
## ---------------------------------------------------------------------------
## Handle Broker request and invoke the registered 'flex' command
## ---------------------------------------------------------------------------
class Flex:
def __init__(self):
# Process Broker arguments
self.call = Broker()
self.command = self.call.getCommand()
self.args = self.call.args
self.opts = self.call.getOptions()
# Build table for function pointers
self.cmdFn = {
'snapshot': self.snapshot,
'snapshots': self.snapshots,
'clone': self.clone,
'clones': self.clones,
'test': self.test,
'help': self.help
};
# Call command
if self.command in self.cmdFn:
self.cmdFn[self.command]()
else:
self.usage()
# Builds a new FLEX_SNAP workspace to create the snapshot
def snapshot(self):
# Get options (set defaults)
flex = ""
from_client_name = self.call.getClient()
for o in self.opts:
if o.startswith('-c'):
from_client_name = o[2:]
else:
flex = o
if not flex:
print("action: REJECT")
print("message: \"%s\"" % "No Flex clone name provided")
return
if not self.permission():
print("action: REJECT")
print("message: \"%s\"" % "You don't have permission for this operation.")
return
try:
# Perforce connection as Flex
p4 = P4Flex().getP4()
from_client = p4.fetch_client(from_client_name)
root = from_client['Root']
# Clone client workspace
flex_client_name = FLEX_SNAP + flex
p4.client = flex_client_name
flex_client = p4.fetch_client("-t", from_client_name, flex_client_name)
# Mirror the same root
flex_client['Root'] = root
flex_client['Host'] = ""
p4.save_client(flex_client)
# Populate have list
p4.run_sync("-k", "//" + flex_client_name + "/...@" + from_client_name)
# Create parent snapshot
netapp = NaFlex()
netapp.create(flex, "1G")
print("action: RESPOND")
print("message: \"%s\"" % netapp)
except P4Exception:
print("action: RESPOND")
error = '\n'.join(p4.errors)
error += '\n'.join(p4.warnings)
print("message: \"%s\"" % error)
finally:
p4.disconnect()
def permission(self):
try:
# Perforce connection as Caller
p4 = self.call.getP4()
for p in p4.run_protects():
if (p['perm'] == 'admin') or (p['perm'] == 'super'):
return True
return False
except P4Exception:
print("action: RESPOND")
error = '\n'.join(p4.errors)
error += '\n'.join(p4.warnings)
print("message: \"%s\"" % error)
finally:
p4.disconnect()
def snapshots(self):
try:
# Perforce connection as Caller
p4 = self.call.getP4()
snapshots = p4.run_clients("-e", FLEX_SNAP + "*")
list = ""
for v in snapshots:
name = v['client'][len(FLEX_SNAP):]
list += "snapshot " + name + " root " + v['Root'] + "\n"
print("action: RESPOND")
print("message: \"%s\"" % list)
except P4Exception:
print("action: RESPOND")
error = '\n'.join(p4.errors)
error += '\n'.join(p4.warnings)
print("message: \"%s\"" % error)
finally:
p4.disconnect()
# Clones a new FLEX_CLONE workspace from the parent flexSnap_
def clone(self):
# Get options
clone = ""
parent_name = ""
for o in self.opts:
if o.startswith('-d'):
del_name = o[2:]
self.clone_del(del_name)
return
if o.startswith('-P'):
parent_name = o[2:]
else:
clone = o
if not clone:
print("action: REJECT")
print("message: \"%s\"" % "No Flex clone name provided")
return
if not parent_name:
print("action: REJECT")
print("message: \"%s\"" % "No Flex parent name provided")
return
try:
# Perforce connection as Caller
p4 = self.call.getP4()
# Verify parent client exists
parent_client_name = FLEX_SNAP + parent_name
list = p4.run_clients("-e", parent_client_name)
if len(list) != 1:
print("action: REJECT")
print("message: \"Flex parent %s does not exist.\"" % parent_client_name)
return
# Clone client workspace
clone_client_name = FLEX_CLONE + clone
p4.client = clone_client_name
clone_client = p4.fetch_client("-t", parent_client_name, clone_client_name)
p4.save_client(clone_client)
# Create flex clone
netapp = ("vol clone create -vserver perforce -flexclone %s -snapshot %s\n"
"vol mount -vserver perforce -snapshot %s\n"
% (clone, parent_name, parent_name))
# Populate have list
p4.run_sync("-k", "//" + clone_client_name + "/...@" + parent_client_name)
print("action: RESPOND")
print("message: \"%s\"" % netapp)
except P4Exception:
print("action: RESPOND")
error = '\n'.join(p4.errors)
error += '\n'.join(p4.warnings)
print("message: \"%s\"" % error)
finally:
p4.disconnect()
print("action: RESPOND")
print("message: \"%s\"" % self.args)
def clones(self):
# Get options
all = False
for o in self.opts:
if o == "-a":
all = True
try:
# Perforce connection as Caller
p4 = self.call.getP4()
clones = []
if all:
clones = p4.run_clients("-e", FLEX_CLONE + "*")
else:
user = self.call.getUser()
clones = p4.run_clients("-e", FLEX_CLONE + "*", "-u", user)
list = ""
for c in clones:
name = c['client'][len(FLEX_CLONE):]
list += "Clone " + name + " root " + c['Root'] + "\n"
print("action: RESPOND")
print("message: \"%s\"" % list)
except P4Exception:
print("action: RESPOND")
error = '\n'.join(p4.errors)
error += '\n'.join(p4.warnings)
print("message: \"%s\"" % error)
finally:
p4.disconnect()
def clone_del(self, clone):
if not clone:
print("action: REJECT")
print("message: \"%s\"" % "No Flex clone name provided")
return
try:
# Perforce connection as Caller
p4 = self.call.getP4()
client_name = FLEX_CLONE + clone
p4.run_client("-d", client_name)
print("action: RESPOND")
print("message: \"Deleted Flex clone %s\"" % clone)
except P4Exception:
print("action: RESPOND")
error = '\n'.join(p4.errors)
error += '\n'.join(p4.warnings)
print("message: \"%s\"" % error)
finally:
p4.disconnect()
def help(self):
help = (
"(FlexClone)\n\n"
" flex -- Perforce FlexClone operations\n\n"
" p4 flex snapshot [-c client] name\n"
" p4 flex snapshots \n"
" p4 flex clone -P parent name\n"
" p4 flex clone -d name\n"
" p4 flex clones [-P parent]\n"
" \n"
" 'p4 flex snapshot' will create a new snapshot.\n"
" \n"
" 'p4 flex snapshots' will display a list of all flex snapshots.\n"
" \n"
" 'p4 flex clone' will create a new flex clone. The '-d' flag will\n"
" delete the flex clone and ascociated client.\n"
" \n"
" 'p4 flex clones' will display a list of all flex clones owned by that\n"
" user. The '-a' flag will list all flex clones globally.\n"
)
print("action: RESPOND")
print("message: \"%s\"" % help)
def test(self):
# Create parent snapshot
netapp = NaFlex()
netapp.create("del2", "1G")
print("action: RESPOND")
print("message: \"test done\"")
def usage(self):
usage = (
"Usage: flex { snapshot | snapshots | clone | clones }\n"
"Missing/wrong number of arguments."
)
print("action: RESPOND")
print("message: \"%s\"" % usage)
## ---------------------------------------------------------------------------
## ---------------------------------------------------------------------------
## MAIN
## ---------------------------------------------------------------------------
if __name__ == '__main__':
flex = Flex()
| # | Change | User | Description | Committed | |
|---|---|---|---|---|---|
| #37 | 18997 | netapp |
Rename/move file(s) Project name change: moving //guest/netapp/p4flexclone/main/… to //guest/netapp/p4flex/main/… |
||
| #36 | 15417 | Paul Allen | Remove client lock. | ||
| #35 | 15070 | Paul Allen | spelling | ||
| #34 | 15068 | Paul Allen | Allow locking on snapshot workspaces and use '-f' on volume delete. | ||
| #33 | 15067 | Paul Allen |
Disable workspace locking for demo. Better solution is for user 'flex' to delete with a '-f', but check ownership first. |
||
| #32 | 15066 | Paul Allen | Update create clone message. | ||
| #31 | 15065 | Paul Allen | Back out changelist 15064 | ||
| #30 | 15064 | Paul Allen | chown fix? | ||
| #29 | 15063 | Paul Allen | Remove description from 'flex volumes' output. | ||
| #28 | 15062 | Paul Allen |
Use Flex user to login current user (needs super) Minor fix to help |
||
| #27 | 15027 | agnesj | Added a check if NetApp volume is mounted, removed test function, and modified -P to -S for flex clone command. | ||
| #26 | 14926 | Paul Allen | Move chown operations to method and use system call to 'chown -R' | ||
| #25 | 14925 | Paul Allen | Generate a P4CONFIG file in the mounted clone's volume. | ||
| #24 | 14924 | Paul Allen | Set workspace 'Root' to mount location for clone and snapshot. | ||
| #23 | 14923 | Paul Allen |
Show Owner when listing clones and volume when listing snapshots. Includes a tidy up on all listing commands. |
||
| #22 | 14921 | Paul Allen |
#review-14917 Verify delete permission for volume/clone. - Only a Perforce 'admin' can delete a volume - Only a Perforce 'admin' and the clone's owner can delete a clone. Use Perforce client workspace based permissions to enforce rules. If the client delete succeeds assume all is ok to delete the volume/clone. Example error: bob$ p4 flex clone -d clone_2_1_a Locked client 'flexClone:clone_2_1_a' owned by 'pallen'; use -f to force update. |
||
| #21 | 14895 | agnesj |
Modified volume function such that when creating a volume the owner of volume is modified. Also modified vol_del and clone_del functions to check for ownership of volume. Only owners of volumes are allowed to delete. |
||
| #20 | 14881 | agnesj |
Added checks in chown and volume functions. And added location of newly created volume in message. |
||
| #19 | 14180 | Paul Allen |
Set file ownership for clone's mounted files. + Minor fix to clone delete |
||
| #18 | 14179 | Paul Allen | Add support for 'flex volumes' command. | ||
| #17 | 14178 | agnesj | Modified NetApp delete to check if volume is a root volume (vserver or node) and to not allow p4 user to delete. | ||
| #16 | 14141 | agnesj | Added definition functions for volume list, snapshot list based on volume name, all snapshot list, clone list | ||
| #15 | 13997 | Paul Allen | Stop on error (don't delete clients if vol delete fails). | ||
| #14 | 13996 | Paul Allen | Added -s to volume for size. | ||
| #13 | 13993 | Paul Allen |
Added NetApp commands to Flex. Example: 4007::ws$ p4 flex volume vol1 Created flex volume vol1 4007::ws$ p4 flex snapshot -V vol1 snapshot1 Created flex snapshot snapshot1 4007::ws$ p4 flex snapshot -V vol1 snapshot2 Created flex snapshot snapshot2 4007::ws$ p4 flex snapshots snapshot vol1:snapshot1 root /home/pallen/flexclone/demo/ws snapshot vol1:snapshot2 root /home/pallen/flexclone/demo/ws 4007::ws$ p4 flex clone -V vol1 -P snapshot1 clone_A Created flex clone client flexClone:clone_A 4007::ws$ p4 flex clone -V vol1 -P snapshot1 clone_B Created flex clone client flexClone:clone_B 4007::ws$ p4 flex clones Clone clone_A root /home/pallen/flexclone/demo Clone clone_B root /home/pallen/flexclone/demo 4007::ws$ p4 clients Client flexClone:clone_A 2015/06/17 root /home/pallen/flexclone/demo 'Created by pallen. ' Client flexClone:clone_B 2015/06/17 root /home/pallen/flexclone/demo 'Created by pallen. ' Client flexSnap:vol1:snapshot1 2015/06/17 root /home/pallen/flexclone/demo/ws 'Created by flex. ' Client flexSnap:vol1:snapshot2 2015/06/17 root /home/pallen/flexclone/demo/ws 'Created by flex. ' Client pallen-ws 2015/06/10 root /home/pallen/flexclone/demo/ws 'Created by pallen. ' 4007::ws$ p4 flex clone -d clone_B Deleted Flex clone clone_B 4007::ws$ p4 flex clone -d clone_A Deleted Flex clone clone_A 4007::ws$ p4 flex volume -d vol1 Deleted Flex volume vol1 deleted client: flexSnap:vol1:snapshot1 deleted client: flexSnap:vol1:snapshot2 4007::ws$ p4 clients Client pallen-ws 2015/06/10 root /home/pallen/flexclone/demo/ws 'Created by pallen. ' |
||
| #12 | 13988 | Paul Allen | Tidy up formatting and imports. | ||
| #11 | 13985 | agnesj |
Added definitions: clone, snapshot, and delete Modified test functions to test each of the added functions |
||
| #10 | 13867 | agnesj | Made corrections to NaFlex Class to pull info from flex.cfg file | ||
| #9 | 13859 | Paul Allen | Config details for cloud demo + NaServer test | ||
| #8 | 13796 | Paul Allen |
Moved options to a configuration file. - The config file 'flex.cfg' MUST be located in the same dir as the script - Provided a sample 'in.txt' file to simulate a broker call (used for debug only) |
||
| #7 | 13791 | Paul Allen | Changed 'volume' to 'snapshot' and minor fix to for auth/login. | ||
| #6 | 13702 | Paul Allen | Added Permission check for p4 flex volume command. | ||
| #5 | 13671 | Paul Allen |
Added '-a' option to flex clones command. Tidy up and hide flexVol/flexClone prefix from user. |
||
| #4 | 13669 | Paul Allen | Added '-d' option to delete flex clone client. | ||
| #3 | 13668 | Paul Allen | Added Perforce functionality for volume(s) and clone(s) commands. | ||
| #2 | 13628 | Paul Allen | set executable | ||
| #1 | 13627 | Paul Allen | New Python script with Implemented help/usage |