This repository has been archived by the owner on May 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 566
/
site_.js
1162 lines (1003 loc) · 39.4 KB
/
site_.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) Microsoft. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var common = require('../common');
var fs = require('fs');
var path = require('path');
var url = require('url');
var util = require('util');
var crypto = require('crypto');
var pfx2pem = require('../../util/certificates/pkcs').pfx2pem;
var Channel = require('../channel');
var async = require('async');
var child_process = require('child_process');
var utils = require('../utils');
var constants = require('../constants');
var cacheUtils = require('../cacheUtils');
var js2xml = require('../../util/js2xml');
var linkedRevisionControl = require('../linkedrevisioncontrol');
exports.init = function (cli) {
var log = cli.output;
function getChannel() {
var account = cli.category('account');
var managementEndpoint = url.parse(utils.getManagementEndpointUrl(account.managementEndpointUrl()));
var pem = account.managementCertificate();
var host = managementEndpoint.hostname;
var port = managementEndpoint.port;
var channel = new Channel({
host: host,
port: port,
key: pem.key,
cert: pem.cert
}).header('x-ms-version', '2011-02-25');
var proxyString =
process.env.HTTPS_PROXY ||
process.env.https_proxy ||
process.env.ALL_PROXY ||
process.env.all_proxy;
if (proxyString !== undefined) {
var proxyUrl = url.parse(proxyString);
if (proxyUrl.protocol !== 'http:' &&
proxyUrl.protocol !== 'https:') {
// fall-back parsing support XXX_PROXY=host:port environment variable values
proxyUrl = url.parse('http://' + proxyString);
}
channel = channel.add({ proxy: proxyUrl });
}
return channel;
}
var site = cli.category('site')
.description('Commands to manage your web sites');
site.command('list')
.whiteListPowershell()
.description('List your web sites')
.option('-s, --subscription <id>', 'use the subscription id')
.execute(function (options, _) {
var parameters = {
subscription: cli.category('account').lookupSubscriptionId(options.subscription)
};
var sites = site.doSitesGet(parameters, _);
if (sites && sites.length > 0) {
for (var s in sites) {
sites[s] = clean(sites[s]);
}
log.table(sites, function (row, s) {
row.cell('Name', s.Name);
row.cell('State', s.State);
row.cell('Host names', s.HostNames);
});
} else {
log.info('No sites created yet. You can create new sites using "azure site create" or through the portal.');
}
});
function choose(data, callback) {
cli.choose(data, function (x) { callback(undefined, x); });
}
function prompt(label, callback) {
cli.prompt(label, function (x) { callback(undefined, x); });
}
function confirm(label, callback) {
cli.confirm(label, function (x) {
if (!x) {
log.warn('The operation was cancelled by the user');
}
callback(undefined, x);
});
}
site.confirm = confirm;
site.command('create [name]')
.whiteListPowershell()
.description('Create a new web site')
.option('-s, --subscription <id>', 'use the subscription id')
.option('--location <location>', 'the geographic region to create the website')
.option('--hostname <hostname>', 'custom host name to use')
.option('--git', 'configure git on web site and local folder')
.option('-pu, --publishingUsername <publishingUsername>', 'The publishing username for git')
.option('--github', 'configure github on web site and local folder')
.option('--githubusername <username>', 'The github username')
.option('--githubpassword <password>', 'The github password')
.option('--githubrepository <repository>', 'The github repository full name (i.e. user/repository)')
.execute(function (nameArg, options, _) {
var context = {
subscription: cli.category('account').lookupSubscriptionId(options.subscription),
git: options.git,
site: {
name: nameArg,
webspace: options.location,
hostname: options.hostname
},
publishingUser: options.publishingUsername,
github: {
username: options.githubusername,
password: options.githubpassword,
repositoryFullName: options.githubrepository
},
flags: { }
};
if (options.git && options.github) {
throw new Error('Please run the command with either --git or --github options. Not both.')
}
// Start by creating the site
promptForSiteName(_);
determineIfSiteExists(_);
promptForLocation(_);
createSite(_);
// Init git / github linking
if (options.git || options.github) {
if (options.github) {
context.lvcClient = linkedRevisionControl.createClient(cli, 'github');
} else if (options.git) {
context.lvcClient = linkedRevisionControl.createClient(cli, 'git');
}
context.lvcClient.init(context, _);
// Scaffold
copyIisNodeWhenServerJsPresent(_);
updateLocalConfigWithSiteName(_);
initializeRemoteRepo(_);
if (options.git) {
addRemoteToLocalGitRepo(_);
} else if (options.github) {
site.ensureRepositoryUri(context, _);
}
context.lvcClient.deploy(context, _);
}
function promptForSiteName(_) {
log.silly('promptForSiteName');
if (context.site.name === undefined) {
log.help('Need a site name');
context.site.name = prompt('Name: ', _);
}
}
function determineIfSiteExists(_) {
log.silly('determineIfSiteExists');
var sites = site.doSitesGet(context, _);
var hits = sites.filter(function (item) {
return utils.ignoreCaseEquals(item.Name, context.site.name);
});
if (hits.length === 1) {
log.info('Updating existing site');
context.flags.siteExists = true;
if (context.site.webspace === undefined) {
context.site.webspace = hits[0].WebSpace;
log.verbose('Existing site location is ', context.site.webspace)
} else {
ensureSpaces(context, _);
var displayNameMatches = context.spaces.filter(function (space) {
return space.GeoRegion === context.site.webspace;
})[0];
if (displayNameMatches && displayNameMatches.Name !== hits[0].WebSpace) {
throw new Error('Expected location ' + context.site.webspace + ' but was ' + displayNameMatches.GeoRegion);
}
}
}
}
function promptForLocation(_) {
log.silly('promptForLocation');
ensureSpaces(context, _);
if (context.site.webspace !== undefined) {
// Map user-provided value to GeoRegion display name, if unique match exists
var displayNameMatches = context.spaces.filter(function (space) {
return space.GeoRegion === context.site.webspace;
});
if (displayNameMatches.length === 1) {
context.site.webspace = displayNameMatches[0].Name;
}
}
if (context.site.webspace !== undefined) {
// Don't prompt if location has been inferred or provided
return;
}
if (context.spaces.length === 0) {
portalCreateSiteInstruction(context, _);
throw new Error('First site must be created on portal');
} else if (context.spaces.length == 1) {
context.site.webspace = context.spaces[0].Name;
log.info('Using location', context.spaces[0].GeoRegion);
} else {
log.help('Choose a region');
context.site.webspace = context.spaces[choose(context.spaces.map(function (space) {
return space.GeoRegion;
}), _)].Name;
}
}
function copyIisNodeWhenServerJsPresent(_) {
log.silly('copyWebConfigWhenServerJsPresent');
if (!utils.pathExistsSync('iisnode.yml') && (utils.pathExistsSync('server.js') || utils.pathExistsSync('app.js'))) {
log.info('Creating default iisnode.yml file');
var sourcePath = path.join(__dirname, '../templates/node/iisnode.yml');
fs.writeFile('iisnode.yml', fs.readFile(sourcePath, _), _);
}
}
function updateLocalConfigWithSiteName(_) {
log.silly('updateLocalConfigWithSiteName');
if (context.flags.isGitWorkingTree) {
var cfg = site.readConfig(_);
cfg.name = context.site.name;
cfg.webspace = context.site.webspace;
site.writeConfig(cfg, _);
}
}
function createSite(_) {
log.silly('createSite');
if (!context.flags.siteExists) {
site.doSitesPost(context, _);
}
}
function initializeRemoteRepo(_) {
log.silly('InitializeRemoteRepo');
if (!context.flags.siteExists) {
site.doRepositoryPost(context, _);
context.repo = site.doRepositoryGet(context, _);
} else {
context.repo = site.doRepositoryGet(context, _);
if (!context.repo) {
site.doRepositoryPost(context, _);
context.repo = site.doRepositoryGet(context, _);
}
}
log.silly('context.repo', context.repo);
}
function addRemoteToLocalGitRepo(_) {
log.silly('addRemoteToLocalGitRepo');
if (!context.flags.isGitWorkingTree) {
log.info('To create a local git repository to publish to the remote site, please rerun this command with the --git flag: "azure site create ' + ((context.site && context.site.name) || '{site name}') + ' --git".')
return;
}
if (!context.publishingUser) {
context.publishingUsers = site.doPublishingUsersGet(context, _);
context.publishingUser = getPublishingUser(context, _);
}
log.verbose('Detecting git and local git folder');
var remotes = exec('git remote', _);
var azureExists = (remotes.stdout + remotes.stderr).split('\n').some(function (item) {
return item === 'azure';
});
if (azureExists) {
log.verbose('Removing existing azure remote alias');
exec('git remote rm azure', _);
}
var gitUri = getGitUri(context.repo, context.site.name, context.publishingUser);
log.info('Executing `git remote add azure ' + gitUri + '`');
exec('git remote add azure ' + gitUri, _);
}
function ensureSpaces(context, _) {
if (!context.spaces) {
context.spaces = cacheUtils.readSpaces(context, _);
if (!context.spaces || !context.spaces.length) {
context.spaces = site.doSpacesGet(context, _);
}
}
}
});
function portalCreateSiteInstruction(context, _) {
log.help('You must create your first web site using the Windows Azure portal.');
log.help('Please follow these steps in the portal:');
log.help('1. At the bottom of the page, click on New > Web Site > Quick Create');
log.help('2. Type "' + ((context.site && context.site.name) || '{site name}') + '" in the URL field');
log.help('3. Click on "Create Web Site"');
log.help('4. Once the site has been created, click on the site name');
log.help('5. Click on "Set up Git publishing" or "Reset deployment credentials" and setup a publishing username and password. Use those credentials for all new websites you create.');
if (context.git) {
log.help('6. Back in the console window, rerun this command by typing "azure site create {site name} --git"');
}
if (confirm('Launch browser to portal now? (y/n) ', _)) {
log.help('Launching portal.');
var href = utils.getPortalUrl();
common.launchBrowser(href);
}
}
function getPublishingUser(context, _) {
var publishingUsers = toArray(context.publishingUsers);
var filters = publishingUsers.filter(function (item) {
return typeof item === 'string' && item.length <= 64;
});
if (filters.length === 0) {
portalGitInitInstruction(context, _);
throw new Error('Git credentials needs to be setup on the portal');
} else if (publishingUsers.length === 1 && filters.length === 1) {
return filters[0];
}
log.help('Please provide the username for Git deployment.');
log.help('If you don\'t have one, please configure it in the management portal.');
return prompt('Publishing username: ', _);
}
function portalGitInitInstruction(context, _) {
log.help('You must create your git publishing credentials using the Windows Azure portal.');
log.help('Please follow these steps in the portal:');
log.help('1. In the menu on the left select "Web Sites"');
log.help('2. Click on the site named "' + ((context.site && context.site.name) || '{site name}') + '" or any other site');
log.help('3. Click on "Set up Git publishing" or "Reset deployment credentials" and setup a publishing username and password. Use those credentials for all new websites you create.');
if (context.git) {
log.help('4. Back in the console window, rerun this command by typing "azure site create {site name} --git"');
}
if (confirm('Launch browser to portal now? (y/n) ', _)) {
log.help('Launching portal.');
var href = utils.getPortalUrl();
common.launchBrowser(href);
}
}
var location = site.category('location')
.description('Commands to manage your Azure locations');
location.command('list')
.whiteListPowershell()
.description('List locations available for your account')
.execute(function (options, _) {
var context = {
subscription: cli.category('account').lookupSubscriptionId(options.subscription)
};
var spaces = site.doSpacesGet(context, _);
if (spaces && spaces.length) {
for (var s in spaces) {
spaces[s] = clean(spaces[s]);
}
log.table(spaces, function (row, item) {
row.cell('Name', item.GeoRegion);
});
} else {
portalCreateSiteInstruction(context, _);
}
});
site.command('portal [name]')
.whiteListPowershell()
.description('Opens the portal in a browser to manage your web sites')
.option("-r, --realm <realm>", "specifies organization used for login")
.execute(function (name, options, _) {
var href = url.parse(utils.getPortalUrl(), true);
delete href.search;
delete href.path;
if (name) {
href.hash = '#Workspaces/WebsiteExtension/Website/' + name + '/dashboard';
}
if(options && options.realm){
href.query.whr = options.realm;
}
targetUrl = url.format(href);
common.launchBrowser(targetUrl);
});
site.command('browse [name]')
.whiteListPowershell()
.description('Open your web site in a browser.')
.option('-s, --subscription <id>', 'use the subscription id')
.execute(function (name, options, _) {
var context = {
subscription: cli.category('account').lookupSubscriptionId(options.subscription),
site: {
name: name
}
};
var cache = lookupSiteNameAndWebSpace(context, _);
var siteData = clean(cache || site.doSiteGet(context, _));
var href = 'http://' + toArray(siteData.HostNames)[0];
common.launchBrowser(href);
});
site.command('show [name]')
.whiteListPowershell()
.description('Show details for a web site')
.option('-s, --subscription <id>', 'use the subscription id')
.execute(function (name, options, _) {
var context = {
subscription: cli.category('account').lookupSubscriptionId(options.subscription),
site: {
name: name
}
};
lookupSiteNameAndWebSpace(context, _);
log.info('Showing details for site');
log.verbose('Parameters', context);
var result = async.parallel([
function (_) { return site.doSiteGet(context, _); },
function (_) { return site.doSiteConfigGet(context, _); }
],
_);
var repositoryUri = getRepositoryUri(result[0]);
var gitUri = repositoryUri ? getGitUri(repositoryUri, context.site.name) : 'none';
var settings = [];
if (repositoryUri) {
site.ensureRepositoryUri(context, _);
settings = site.category('repository').doSettingsGet(context, _);
}
if (log.format().json) {
var data = {
site: clean(result[0]),
config: clean(result[1]),
gitRepositoryUri: gitUri,
settings: settings
};
log.json(data);
} else {
logEachData('Site', result[0]);
logEachData('Config', result[1]);
log.data('GitRepositoryUri', gitUri);
for (var index in settings) {
log.data('Settings' + ' ' + settings[index].Key, settings[index].Value);
}
}
});
function lookupSiteName(context, _) {
if (context.site.name !== undefined) {
// no need to read further
return;
}
var cfg = site.readConfig(_);
if (cfg && cfg.name) {
// using the name from current location
context.site.name = cfg.name;
context.site.webspace = cfg.webspace;
return;
}
context.site.name = prompt('Web site name: ', _);
if (!context.site.name) {
throw new Error('Invalid site name');
}
}
function lookupSiteWebSpace(context, _) {
log.verbose('Attempting to locate site ', context.site.name);
var sites = site.doSitesGet(context, _);
for (var index in sites) {
if (utils.ignoreCaseEquals(sites[index].Name, context.site.name)) {
log.verbose('Site located at ', sites[index].WebSpace);
context.site.webspace = sites[index].WebSpace;
}
}
if (context.site.webspace === undefined) {
throw new Error('Unable to locate site named ' + context.site.name);
}
}
function lookupSiteNameAndWebSpace(context, _) {
lookupSiteName(context, _);
var cache = cacheUtils.readSite(context, _);
if (cache || context.site.webspace) {
context.site.webspace = (cache && cache.WebSpace) || context.site.webspace;
return cache;
}
lookupSiteWebSpace(context, _);
}
site.lookupSiteNameAndWebSpace = lookupSiteNameAndWebSpace;
function getRepositoryUri(siteData) {
if (siteData.SiteProperties.Properties) {
for (var i = 0; i < siteData.SiteProperties.Properties.NameValuePair.length; ++i) {
var pair = siteData.SiteProperties.Properties.NameValuePair[i];
if (utils.ignoreCaseEquals(pair.Name, "RepositoryUri")) {
if (typeof pair.Value === 'string') {
if (!endsWith(pair.Value, '/')) {
// Make sure there is a trailing slash
pair.Value += '/';
}
return pair.Value;
} else {
return null;
}
}
}
}
return null;
}
site.getRepositoryUri = getRepositoryUri;
function getGitUri(repositoryUri, siteName, auth) {
var repoUrl = url.parse(repositoryUri);
if (auth) {
repoUrl.auth = auth;
}
var sitePath = siteName + '.git';
if (!endsWith(repoUrl.path, '/')) {
// Make sure trailing slash exists
repoUrl.path += '/';
}
repoUrl.path += sitePath;
if (!endsWith(repoUrl.pathname, '/')) {
// Make sure trailing slash exists
repoUrl.pathname += '/';
}
repoUrl.pathname += sitePath;
return url.format(repoUrl);
}
function getRepositoryAuth(siteData) {
var userName, password;
for (var i = 0; i < siteData.SiteProperties.Properties.NameValuePair.length; ++i) {
var pair = siteData.SiteProperties.Properties.NameValuePair[i];
if (utils.ignoreCaseEquals(pair.Name, "PublishingUsername")) {
userName = pair.Value;
} else if (utils.ignoreCaseEquals(pair.Name, "PublishingPassword")) {
password = pair.Value;
}
}
return userName && (userName + ":" + password);
}
site.getRepositoryAuth = getRepositoryAuth;
function ensureRepositoryUri(context, _) {
var siteData = site.lookupSiteNameAndWebSpace(context, _);
var repositoryUri = siteData && site.getRepositoryUri(siteData);
if (!repositoryUri) {
siteData = site.doSiteGet(context, _);
repositoryUri = site.getRepositoryUri(siteData);
}
if (repositoryUri) {
context.repositoryAuth = site.getRepositoryAuth(siteData);
return context.repositoryUri = repositoryUri;
}
}
site.ensureRepositoryUri = ensureRepositoryUri;
site.command('delete [name]')
.whiteListPowershell()
.description('Delete a web site')
.option('-s, --subscription <id>', 'use the subscription id')
.option('-q, --quiet', 'quiet mode, do not ask for delete confirmation')
.execute(function (name, options, _) {
var context = {
subscription: cli.category('account').lookupSubscriptionId(options.subscription),
site: {
name: name
}
};
lookupSiteNameAndWebSpace(context, _);
log.info('Deleting site', context.site.name);
if (!options.quiet && !confirm('Delete ' + context.site.name + ' site? (y/n) ', _)) {
return;
}
var progress = cli.progress('Deleting site');
try {
var result = getChannel()
.path(context.subscription)
.path('services')
.path('webspaces')
.path(context.site.webspace)
.path('sites')
.path(context.site.name)
.DELETE(_);
cacheUtils.deleteSite(context, _);
}
finally {
progress.end();
}
log.info('Site ' + context.site.name + ' has been deleted');
});
site.command('start [name]')
.whiteListPowershell()
.description('Start a web site')
.option('-s, --subscription <id>', 'use the subscription id')
.execute(function (name, options, _) {
var context = {
subscription: cli.category('account').lookupSubscriptionId(options.subscription),
site: {
name: name
}
};
lookupSiteNameAndWebSpace(context, _);
log.info('Starting site', context.site.name);
updateSiteState(getChannel(), context, 'Running', _);
log.info('Site ' + context.site.name + ' has been started');
});
site.command('stop [name]')
.whiteListPowershell()
.description('Stop a web site')
.option('-s, --subscription <id>', 'use the subscription id')
.execute(function (name, options, _) {
var context = {
subscription: cli.category('account').lookupSubscriptionId(options.subscription),
site: {
name: name
}
};
lookupSiteNameAndWebSpace(context, _);
log.info('Stopping site', context.site.name);
updateSiteState(getChannel(), context, 'Stopped', _);
log.info('Site ' + context.site.name + ' has been stopped');
});
site.command('restart [name]')
.whiteListPowershell()
.description('Stop and then start a web site')
.option('-s, --subscription <id>', 'use the subscription id')
.execute(function (name, options, _) {
var context = {
subscription: cli.category('account').lookupSubscriptionId(options.subscription),
site: {
name: name
}
};
lookupSiteNameAndWebSpace(context, _);
log.info('Stopping site', context.site.name);
updateSiteState(getChannel(), context, 'Stopped', _);
log.info('Site ' + context.site.name + ' has been stopped, restarting');
updateSiteState(getChannel(), context, 'Running', _);
log.info('Site ' + context.site.name + ' has been restarted');
});
function updateSiteState(channel, context, newState, _)
{
var progress = cli.progress('Updating site state');
try {
var result = channel
.path(context.subscription)
.path('services')
.path('webspaces')
.path(context.site.webspace)
.path('sites')
.path(context.site.name)
.header('Content-Type', 'application/xml')
.PUT(function (req) {
req.write('<Site xmlns="http://schemas.microsoft.com/windowsazure">');
req.write('<HostNames>');
req.write('<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">');
req.write(context.site.name + utils.getHostNameSuffix());
req.write('</string>');
req.write('</HostNames>');
req.write('<Name>');
req.write(context.site.name);
req.write('</Name>');
req.write('<State>');
req.write(newState);
req.write('</State>');
req.write('</Site>');
req.end();
}, _);
}
finally {
progress.end();
}
}
/////////////////
// config and settings
site.readConfig = function (_) {
return {
name: site.readConfigValue('azure.site.name', _),
webspace: site.readConfigValue('azure.site.webspace', _)
};
};
site.writeConfig = function (cfg, _) {
site.writeConfigValue('azure.site.name', cfg.name, _);
site.writeConfigValue('azure.site.webspace', cfg.webspace, _);
};
site.readConfigValue = function (name, _) {
try {
var result = exec('git config --get ' + name, _);
return (result.stdout + result.stderr).trim();
}
catch (err) {
log.silly('Unable to read config', err);
return '';
}
};
site.writeConfigValue = function (name, value, _) {
exec('git config ' + name + ' ' + value, _);
};
/////////////////
// remote api operations
site.doSitesPost = function (options, callback) {
log.info('Creating a new web site at ' + options.site.name + utils.getHostNameSuffix());
log.verbose('Subscription', options.subscription);
log.verbose('Webspace', options.site.webspace);
log.verbose('Site', options.site.name);
var progress = cli.progress('Sending site information');
getChannel()
.path(options.subscription)
.path('services')
.path('webspaces')
.path(options.site.webspace)
.path('sites')
.header('Content-Type', 'application/xml')
.POST(
writers.Site.xml(options.site),
function (err, result) {
progress.end();
if (err) {
logError('Failed to create site', err);
} else {
return cacheUtils.saveSite(options, result, function (err) {
log.info('Created website at ' + clean(result).HostNames);
log.verbose('Site', clean(result));
return callback(err, result);
});
}
if (err && typeof err.Message !== 'string') {
return callback('Invalid service request', result);
} else {
return callback(err, result);
}
});
};
site.doRepositoryPost = function (options, callback) {
log.info('Initializing remote Azure repository');
log.verbose('Subscription', options.subscription);
log.verbose('Webspace', options.site.webspace);
log.verbose('Site', options.site.name);
var progress = cli.progress('Updating site information');
getChannel()
.path(options.subscription)
.path('services')
.path('webspaces')
.path(options.site.webspace)
.path('sites')
.path(options.site.name)
.path('repository')
.POST(
"",
function (err, result) {
progress.end();
if (err) {
logError('Failed to initialize repository', err);
} else {
log.info('Remote azure repository initialized');
}
return callback(err, result);
});
};
site.doRepositoryDelete = function(options, callback) {
log.verbose('Subscription', options.subscription);
log.verbose('Webspace', options.site.webspace);
log.verbose('Site', options.site.name);
var progress = cli.progress('Updating site information');
getChannel()
.path(options.subscription)
.path('services')
.path('webspaces')
.path(options.site.webspace)
.path('sites')
.path(options.site.name)
.path('repository')
.DELETE(
function(err, result) {
progress.end();
if (err) {
logError('Failed to delete repository', err);
} else {
log.info('Repository deleted');
}
return callback(err, result);
});
};
site.doSpacesGet = function (options, _) {
log.verbose('Subscription', options.subscription);
var progress = cli.progress('Enumerating locations');
try {
var result = getChannel()
.path(options.subscription)
.path('services')
.path('webspaces')
.path('')
.GET(_);
log.json('silly', result);
var spaces = toArray(result.WebSpace);
cacheUtils.saveSpaces(options, spaces, _);
return spaces;
}
finally {
progress.end();
}
};
site.doSitesGet = function (options, _) {
log.verbose('Subscription', options.subscription);
var spaces = site.doSpacesGet(options, _);
var channel = getChannel()
.path(options.subscription)
.path('services')
.path('webspaces');
var progress = cli.progress('Enumerating sites');
try {
var result = async.map(
spaces,
function (webspace, _) {
return channel
.path(webspace.Name)
.path('sites')
.path('')
.query('propertiesToInclude', 'repositoryuri,publishingpassword,publishingusername')
.GET(_);
},
_);
var sites = [];
result.forEach(function (item) {
sites = sites.concat(toArray(item.Site));
});
result = sites;
log.json('verbose', sites);
cacheUtils.saveSites(options, result, _);
return sites;
}
finally {
progress.end();
}
};
site.doSiteGet = function (options, callback) {
var progress = cli.progress('Retrieving site information');
getChannel()
.path(options.subscription)
.path('services')
.path('webspaces')
.path(options.site.webspace)
.path('sites')
.path(options.site.name)
.query('propertiesToInclude', 'repositoryuri,publishingpassword,publishingusername')
.GET(
function (err, result) {
progress.end();
if (err) {
logError('Failed to get site info', err);
if (err.Code === 'NotFound') {
return cacheUtils.deleteSite(options, function () {
return callback(err, result);
});
}
} else {
return cacheUtils.saveSite(options, result, function (err) {
log.verbose('Site', clean(result));
return callback(err, result);
});
}
return callback(err, result);
});
};
site.doSiteConfigGet = function (options, callback) {
var progress = cli.progress('Retrieving site config information');
getChannel()
.path(options.subscription)
.path('services')
.path('webspaces')
.path(options.site.webspace)
.path('sites')
.path(options.site.name)
.path('config')
.GET(
function (err, result) {
progress.end();
if (err) {
logError('Failed to get site config info', err);
} else {
log.verbose('SiteConfig', clean(result));
}
return callback(err, result);
});
};
site.doSiteConfigPUT = function (config, options, _) {
var progress = cli.progress('Updating site config information');