public static void InstallZipOnCloudshare(string filename, string blueprintName, string snapshotName)
        {
            var ac = new CloudshareAccount();

            ac.ApiId     = ConfigurationManager.AppSettings["ApiId"];
            ac.ApiKey    = ConfigurationManager.AppSettings["ApiKey"];
            ac.ServerUrl = ConfigurationManager.AppSettings["ServerUrl"];

            client = new CloudshareAPIWrapper.CloudshareClient(ac);

            var environmentStates = CloudshareManager.client.GetAllAvailableBlueprints();

            cloudFoldersInfo = CloudshareManager.client.GetCloudFoldersInfo();

            var blueprints = (from data in environmentStates.data
                              from blueprint in data.Blueprints
                              orderby blueprint.Name
                              select blueprint.Name).Distinct().ToList();

            var snapshots = (from data in environmentStates.data
                             from blueprint in data.Blueprints
                             from snapshot in blueprint.Snapshots
                             where blueprint.Name == blueprintName
                             orderby snapshot.Name
                             select snapshot.Name).ToList();

            var selectedSnapshot = (from data in environmentStates.data
                                    from blueprint in data.Blueprints
                                    from snapshot in blueprint.Snapshots
                                    where snapshot.IsLatest == true && blueprint.Name == blueprintName
                                    select snapshot).FirstOrDefault();
            var selectedBluprint = (from data in environmentStates.data
                                    from blueprint in data.Blueprints
                                    from snapshot in blueprint.Snapshots
                                    where snapshot.IsLatest == true && blueprint.Name == blueprintName
                                    select blueprint).FirstOrDefault();

            var selectedData = (from data in environmentStates.data
                                from blueprint in data.Blueprints
                                from snapshot in blueprint.Snapshots
                                where snapshot.IsLatest == true && blueprint.Name == blueprintName
                                select data).FirstOrDefault();

            CloudshareManager.client = new CloudshareAPIWrapper.CloudshareClient(ac);
            var environmentName = string.Format("CloudahreAutomation{0}", DateTime.Now.Ticks);
            var a = CloudshareManager.client.CreateBlueprintFromSnapshot(selectedData.EnvironmentPolicyId, selectedSnapshot.SnapshotId, environmentName, selectedBluprint.Name, selectedData.EnvironmentPolicyDuration);

            CloudshareManager.UploadFile(filename);

            var env   = client.GetEnvironmentsList();
            var envId = (from data in env.data
                         where data.name == environmentName
                         select data.envId).FirstOrDefault();

            var networkFolder = CloudshareManager.client.Mount(envId);

            var vms  = client.GetEnvironmentState(envId);
            var vmId = (from dat in vms.data.vms
                        select vms.data.vms[0].vmId).FirstOrDefault();

            var fileName = Path.GetFileName(filename);

            var req = WebRequest.Create(@"http://" + vms.data.vms[0].FQDN + @"/CloudshareAgent/Install.ashx?zip=" + fileName + @"&subfolder=" + CloudshareManager.remoteUserFolderName.Replace(" ", "-").Replace("'", "-").Replace("(", "-").Replace(")", "-")
                                        + @"&username="******"&password="******"GET";

            //TODO: response checker if anything is OK
            var response = req.GetResponse();

            var executedPath = client.ExecutePath(envId, vmId, @"c:\installer\Jetstream.bat");

            Thread.Sleep(1800000);

            var resp = client.TakeSnapshot(envId, "Jestream " + DateTime.Now.ToString("yyyyMMdd"), "Jetstream cloudshare created on " + DateTime.Now.ToShortDateString(), false);

            Console.WriteLine(resp.status_text);
        }
        public static void InstallZipOnCloudshare(string filename, string blueprintName, string snapshotName)
        {
            var ac = new CloudshareAccount();
            ac.ApiId = ConfigurationManager.AppSettings["ApiId"];
            ac.ApiKey = ConfigurationManager.AppSettings["ApiKey"];
            ac.ServerUrl = ConfigurationManager.AppSettings["ServerUrl"];

            client = new CloudshareAPIWrapper.CloudshareClient(ac);

            var environmentStates = CloudshareManager.client.GetAllAvailableBlueprints();

            cloudFoldersInfo = CloudshareManager.client.GetCloudFoldersInfo();

            var blueprints = (from data in environmentStates.data
                              from blueprint in data.Blueprints
                              orderby blueprint.Name
                              select blueprint.Name).Distinct().ToList();

            var snapshots = (from data in environmentStates.data
                             from blueprint in data.Blueprints
                             from snapshot in blueprint.Snapshots
                             where blueprint.Name == blueprintName
                             orderby snapshot.Name
                             select snapshot.Name).ToList();

            var selectedSnapshot = (from data in environmentStates.data
                                    from blueprint in data.Blueprints
                                    from snapshot in blueprint.Snapshots
                                    where snapshot.IsLatest == true && blueprint.Name == blueprintName
                                    select snapshot).FirstOrDefault();
            var selectedBluprint = (from data in environmentStates.data
                                    from blueprint in data.Blueprints
                                    from snapshot in blueprint.Snapshots
                                    where snapshot.IsLatest == true && blueprint.Name == blueprintName
                                    select blueprint).FirstOrDefault();

            var selectedData = (from data in environmentStates.data
                                from blueprint in data.Blueprints
                                from snapshot in blueprint.Snapshots
                                where snapshot.IsLatest == true && blueprint.Name == blueprintName
                                select data).FirstOrDefault();

            CloudshareManager.client = new CloudshareAPIWrapper.CloudshareClient(ac);
            var environmentName = string.Format("CloudahreAutomation{0}", DateTime.Now.Ticks);
            var a = CloudshareManager.client.CreateBlueprintFromSnapshot(selectedData.EnvironmentPolicyId, selectedSnapshot.SnapshotId, environmentName, selectedBluprint.Name, selectedData.EnvironmentPolicyDuration);
            CloudshareManager.UploadFile(filename);

            var env = client.GetEnvironmentsList();
            var envId = (from data in env.data
                         where data.name == environmentName
                         select data.envId).FirstOrDefault();

            var networkFolder = CloudshareManager.client.Mount(envId);

            var vms = client.GetEnvironmentState(envId);
            var vmId = (from dat in vms.data.vms
                        select vms.data.vms[0].vmId).FirstOrDefault();

            var fileName = Path.GetFileName(filename);

            var req = WebRequest.Create(@"http://" + vms.data.vms[0].FQDN + @"/CloudshareAgent/Install.ashx?zip=" + fileName + @"&subfolder=" + CloudshareManager.remoteUserFolderName.Replace(" ", "-").Replace("'", "-").Replace("(", "-").Replace(")", "-")
              + @"&username="******"&password="******"GET";

            //TODO: response checker if anything is OK
            var response = req.GetResponse();

            var executedPath = client.ExecutePath(envId, vmId, @"c:\installer\Jetstream.bat");
            Thread.Sleep(1800000);

            var resp = client.TakeSnapshot(envId, "Jestream " + DateTime.Now.ToString("yyyyMMdd"), "Jetstream cloudshare created on " + DateTime.Now.ToShortDateString(), false);
            Console.WriteLine(resp.status_text);
        }
        private void InstallButton_Click(object sender, EventArgs e)
        {
            bpLoadFile.DisplayStyle = ProgressBarDisplayText.CustomText;
            bpLoadFile.CustomText   = "Load package...";

            string environmentName;

            if (rbNewEnvironment.Checked)
            {
                environmentName = string.Format("CloudahreAutomation{0}", DateTime.Now.Ticks);

                //TODO: log handling to listbox
                var selectedSnapshot = (from data in environmentStates.data
                                        from blueprint in data.Blueprints
                                        from snapshot in blueprint.Snapshots
                                        where snapshot.Name == this.SpapshotsComboBox.SelectedItem
                                        select snapshot).FirstOrDefault();
                var selectedBluprint = (from data in environmentStates.data
                                        from blueprint in data.Blueprints
                                        from snapshot in blueprint.Snapshots
                                        where snapshot.Name == this.SpapshotsComboBox.SelectedItem
                                        select blueprint).FirstOrDefault();

                var selectedData = (from data in environmentStates.data
                                    from blueprint in data.Blueprints
                                    from snapshot in blueprint.Snapshots
                                    where snapshot.Name == this.SpapshotsComboBox.SelectedItem
                                    select data).FirstOrDefault();

                if (selectedData != null && selectedSnapshot != null && selectedBluprint != null)
                {
                    var newBlueprint = this.client.CreateBlueprintFromSnapshot(selectedData.EnvironmentPolicyId,
                                                                               selectedSnapshot.SnapshotId,
                                                                               environmentName, selectedBluprint.Name,
                                                                               selectedData.EnvironmentPolicyDuration);
                    if (newBlueprint.status_text == "Success")
                    {
                        lbLogs.Items.Add(string.Format("New Blueprint {0} has been created!", environmentName));
                    }
                    else
                    {
                        lbLogs.Items.Add(string.Format("Create of new Blueprint {0} failed! ", environmentName));
                        lbLogs.Items.Add(string.Format("Exception: {0} ", newBlueprint.status_text));
                    }
                }
            }
            else
            {
                environmentName = BlueprintsComboBox.SelectedItem.ToString();
            }

            this.UploadZip();
            var    env = client.GetEnvironmentsList();
            string envId;

            if (tbOwner.Visible)
            {
                envId = (from data in env.data
                         where data.name == environmentName && data.owner == tbOwner.Text
                         select data.envId).FirstOrDefault();
            }
            else
            {
                envId = (from data in env.data
                         where data.name == environmentName
                         select data.envId).FirstOrDefault();
            }

            if (envId == null)
            {
                return;
            }

            this.networkFolder = this.client.Mount(envId);
            lbLogs.Items.Add("Network Folder has been mounted!");
            var vms  = client.GetEnvironmentState(envId);
            var vmId = (from dat in vms.data.vms
                        select vms.data.vms[0].vmId).FirstOrDefault();

            var req = WebRequest.Create(@"http://" + vms.data.vms[0].FQDN + @"/CloudshareAgent/Install.ashx?zip=" + this.filename + @"&subfolder=" + this.remoteUserFolderName.Replace(" ", "-").Replace("'", "-").Replace("(", "-").Replace(")", "-")
                                        + @"&username="******"&password="******"WebRequest has been made!");
            }

            var executedPath = client.ExecutePath(envId, vmId, @"c:\installer\Jetstream.bat");

            Thread.Sleep(5000);//1800000
            lbLogs.Items.Add("Jetstream.bat has been executed!");
            lbLogs.Items.Add("Status of run Jetstream.bat is:" + executedPath.status_text);
            lbLogs.Items.Add("Done!");
            lbLogs.Items.Add("************************************************************");
        }