Skip to content

Commit 73506b1

Browse files
committed
provision a user
1 parent fb3204b commit 73506b1

File tree

1 file changed

+274
-0
lines changed

1 file changed

+274
-0
lines changed
+274
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Auto-provision a new user\n",
8+
"\n",
9+
"- Create a home directory\n",
10+
"- Create NFS export\n",
11+
"- Create SMB share\n",
12+
"- Set up daily snapshots\n",
13+
"\n",
14+
"### Prerequisites\n",
15+
"\n",
16+
"- Install the qumulo api via `pip install qumulo_api`, or download it from your Qumulo cluster on the API & Tools page\n",
17+
"- set up all the variables in the cell below"
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"execution_count": null,
23+
"metadata": {
24+
"collapsed": true
25+
},
26+
"outputs": [],
27+
"source": [
28+
"cluster = 'XXXXX' # Qumulo cluster hostname or IP where you're setting up users\n",
29+
"api_user = 'XXXXX' # Qumulo api user name\n",
30+
"api_password = 'XXXXX' # Qumulo api password\n",
31+
"base_directory = 'XXXXX' # the parent path where the users will be created.\n",
32+
"\n",
33+
"user_name = 'XXXXX' # the new \"user\" to set up."
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": null,
39+
"metadata": {
40+
"collapsed": true,
41+
"scrolled": true
42+
},
43+
"outputs": [],
44+
"source": [
45+
"import os\n",
46+
"import sys\n",
47+
"import traceback\n",
48+
"\n",
49+
"from qumulo.rest_client import RestClient\n",
50+
"from qumulo.rest.nfs import NFSRestriction"
51+
]
52+
},
53+
{
54+
"cell_type": "code",
55+
"execution_count": null,
56+
"metadata": {
57+
"collapsed": true
58+
},
59+
"outputs": [],
60+
"source": [
61+
"full_path = '/'+ base_dir + '/' + user_name"
62+
]
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": null,
67+
"metadata": {
68+
"collapsed": true
69+
},
70+
"outputs": [],
71+
"source": [
72+
"rc = RestClient(cluster, 8000)\n",
73+
"rc.login(api_user, api_password)"
74+
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": null,
79+
"metadata": {
80+
"collapsed": true
81+
},
82+
"outputs": [],
83+
"source": [
84+
"def create_dir(rc, name, dir_path='/'):\n",
85+
" try:\n",
86+
" rc.fs.create_directory(name = name, dir_path = dir_path)\n",
87+
" except:\n",
88+
" exc_type, exc_value, exc_traceback = sys.exc_info()\n",
89+
" print(\"Exception: %s\" % exc_value)"
90+
]
91+
},
92+
{
93+
"cell_type": "code",
94+
"execution_count": null,
95+
"metadata": {
96+
"collapsed": true
97+
},
98+
"outputs": [],
99+
"source": [
100+
"# Create base user directory, if it doesn't already exist\n",
101+
"create_dir(rc, name=base_dir, dir_path='/')"
102+
]
103+
},
104+
{
105+
"cell_type": "markdown",
106+
"metadata": {},
107+
"source": [
108+
"### Create directory"
109+
]
110+
},
111+
{
112+
"cell_type": "code",
113+
"execution_count": null,
114+
"metadata": {
115+
"collapsed": true
116+
},
117+
"outputs": [],
118+
"source": [
119+
"dir_res = rc.fs.create_directory(name=user_name, dir_path='/'+ base_dir)\n",
120+
"print(\"Directory '%s' created with id: %s\" % (full_path, dir_res['file_number']))\n",
121+
"dir_id = dir_res['file_number']"
122+
]
123+
},
124+
{
125+
"cell_type": "markdown",
126+
"metadata": {},
127+
"source": [
128+
"### Create 20GB Quota"
129+
]
130+
},
131+
{
132+
"cell_type": "code",
133+
"execution_count": null,
134+
"metadata": {
135+
"collapsed": true
136+
},
137+
"outputs": [],
138+
"source": [
139+
"quota_res = rc.quota.create_quota(id_ = dir_id, limit_in_bytes = 20000000000)"
140+
]
141+
},
142+
{
143+
"cell_type": "markdown",
144+
"metadata": {},
145+
"source": [
146+
"### Create NFS export"
147+
]
148+
},
149+
{
150+
"cell_type": "code",
151+
"execution_count": null,
152+
"metadata": {
153+
"collapsed": true
154+
},
155+
"outputs": [],
156+
"source": [
157+
"nfs_res = rc.nfs.nfs_add_share(export_path = '/' + user_name,\n",
158+
" fs_path = full_path,\n",
159+
" description = \"%s home directory\" % user_name,\n",
160+
" restrictions = [NFSRestriction({\n",
161+
" 'read_only': False, \n",
162+
" 'host_restrictions': [],\n",
163+
" 'user_mapping': 'NFS_MAP_NONE', \n",
164+
" 'map_to_user_id': '0'})]\n",
165+
" )\n",
166+
"print(\"NFS export created: %s with id %s\" % (full_path, nfs_res['id']))"
167+
]
168+
},
169+
{
170+
"cell_type": "markdown",
171+
"metadata": {},
172+
"source": [
173+
"### Create SMB share"
174+
]
175+
},
176+
{
177+
"cell_type": "code",
178+
"execution_count": null,
179+
"metadata": {
180+
"collapsed": true
181+
},
182+
"outputs": [],
183+
"source": [
184+
"smb_res = rc.smb.smb_add_share(share_name = user_name, \n",
185+
" fs_path = full_path, \n",
186+
" description = \"%s home directory\" % user_name\n",
187+
" )\n",
188+
"print(\"SMB share created: %s with id %s\" % (full_path, smb_res['id']))"
189+
]
190+
},
191+
{
192+
"cell_type": "markdown",
193+
"metadata": {},
194+
"source": [
195+
"### Set up snapshot policy"
196+
]
197+
},
198+
{
199+
"cell_type": "code",
200+
"execution_count": null,
201+
"metadata": {
202+
"collapsed": true
203+
},
204+
"outputs": [],
205+
"source": [
206+
"snap_res = rc.snapshot.create_policy(name = \"User %s\" % user_name, \n",
207+
" schedule_info = {\"creation_schedule\":\n",
208+
" {\"frequency\":\"SCHEDULE_DAILY_OR_WEEKLY\",\n",
209+
" \"hour\":2,\"minute\":15,\n",
210+
" \"on_days\":[\"MON\",\"TUE\",\"WED\",\"THU\",\"FRI\",\"SAT\",\"SUN\"],\n",
211+
" \"timezone\":\"America/Los_Angeles\"},\n",
212+
" \"expiration_time_to_live\":\"7days\"\n",
213+
" },\n",
214+
" directory_id = str(dir_id),\n",
215+
" enabled = True)\n",
216+
"print(\"Snapshot policy created with id %s\" % snap_res['id'])"
217+
]
218+
},
219+
{
220+
"cell_type": "markdown",
221+
"metadata": {},
222+
"source": [
223+
"### Clean up everything"
224+
]
225+
},
226+
{
227+
"cell_type": "code",
228+
"execution_count": null,
229+
"metadata": {
230+
"collapsed": true
231+
},
232+
"outputs": [],
233+
"source": [
234+
"rc.quota.delete_quota(id_ = quota_res['id'])\n",
235+
"rc.snapshot.delete_policy(policy_id = snap_res['id'])\n",
236+
"rc.smb.smb_delete_share(id_ = smb_res['id'])\n",
237+
"rc.nfs.nfs_delete_share(id_ = nfs_res['id'])\n",
238+
"if full_path != '/': # small sanity check since tree delete is rather powerful.\n",
239+
" rc.fs.delete_tree(path = full_path)\n",
240+
"print(\"Everything is cleaned up!\")"
241+
]
242+
},
243+
{
244+
"cell_type": "code",
245+
"execution_count": null,
246+
"metadata": {
247+
"collapsed": true
248+
},
249+
"outputs": [],
250+
"source": []
251+
}
252+
],
253+
"metadata": {
254+
"kernelspec": {
255+
"display_name": "Python 2",
256+
"language": "python",
257+
"name": "python2"
258+
},
259+
"language_info": {
260+
"codemirror_mode": {
261+
"name": "ipython",
262+
"version": 2
263+
},
264+
"file_extension": ".py",
265+
"mimetype": "text/x-python",
266+
"name": "python",
267+
"nbconvert_exporter": "python",
268+
"pygments_lexer": "ipython2",
269+
"version": "2.7.12"
270+
}
271+
},
272+
"nbformat": 4,
273+
"nbformat_minor": 2
274+
}

0 commit comments

Comments
 (0)