Esempio n. 1
0
 public void RemoveApp(Guid targetKey, Guid appKey)
 {
     try
     {
         var indexes = new Indexes(Context);
         indexes.DeleteIndexEntry(GetTargetAppsIndexPath(targetKey), appKey);
     }
     catch (Exception ex)
     {
         throw new DeploymentException(string.Format("Failed removing app with key \"{0}\" from target \"{1}\".", appKey, targetKey), ex);
     }
 }
Esempio n. 2
0
 public IEnumerable<Guid> GetTargetAppKeys(Guid targetKey)
 {
     try
     {
         var indexes = new Indexes(Context);
         var index = indexes.LoadIndex(GetTargetAppsIndexPath(targetKey));
         return index.Entries.Select(e => e.Key);
     }
     catch (Exception ex)
     {
         throw new DeploymentException(string.Format("Failed getting app keys for target \"{0}\".", targetKey), ex);
     }
 }
Esempio n. 3
0
 public void AddApp(Guid targetKey, Guid appKey)
 {
     try
     {
         var indexes = new Indexes(Context);
         // We don't need the name so just set to a dash.
         indexes.PutIndexEntry(GetTargetAppsIndexPath(targetKey), new EntityIndexEntry() { Key = appKey, Name = "-" });
     }
     catch (Exception ex)
     {
         throw new DeploymentException(string.Format("Failed adding app with key \"{0}\" to target \"{1}\".", appKey, targetKey), ex);
     }
 }
Esempio n. 4
0
        public void DeleteGroup(Guid key)
        {
            try
            {
                Plywood.Internal.AwsHelpers.SoftDeleteFolders(Context, string.Format("{0}/{1}", STR_GROUPS_CONTAINER_PATH, key.ToString("N")));

                var indexesController = new Internal.Indexes(Context);
                indexesController.DeleteIndexEntry(STR_GROUP_INDEX_PATH, key);
            }
            catch (AmazonS3Exception awsEx)
            {
                throw new DeploymentException("Failed deleting group.", awsEx);
            }
        }
Esempio n. 5
0
        public TargetAppList SearchTargetApps(Guid targetKey, string query = null, int offset = 0, int pageSize = 50)
        {
            try
            {
                var indexes = new Indexes(Context);
                var targets = new Targets(Context);
                var apps = new Apps(Context);
                var index = indexes.LoadIndex(GetTargetAppsIndexPath(targetKey));
                var target = targets.GetTarget(targetKey);
                var groupAppIndex = indexes.LoadIndex(Apps.GetGroupAppsIndexPath(target.GroupKey));

                var filteredIndex = index.Entries.AsQueryable();

                if (!string.IsNullOrWhiteSpace(query))
                {
                    var queryParts = query.ToLower().Split(new char[] { ' ', '\t', ',' }).Where(qp => !string.IsNullOrWhiteSpace(qp)).ToArray();
                    filteredIndex = filteredIndex.Where(e => queryParts.Any(q => e.Name.ToLower().Contains(q)));
                }

                var count = filteredIndex.Count();

                var listItems = filteredIndex.Skip(offset).Take(pageSize).ToList().Select(e =>
                    {
                        var groupApp = groupAppIndex.Entries.FirstOrDefault(a => a.Key == e.Key);
                        return new AppListItem()
                        {
                            Key = e.Key,
                            Name = (groupApp != null) ? groupApp.Name : TryResolveAppNameDirect(e.Key, apps)
                        };
                    }).ToList().OrderBy(app => app.Name);

                var list = new TargetAppList()
                {
                    Apps = listItems,
                    TargetKey = targetKey,
                    Query = query,
                    Offset = offset,
                    PageSize = pageSize,
                    TotalCount = count,
                };

                return list;
            }
            catch (Exception ex)
            {
                throw new DeploymentException(string.Format("Failed getting app keys for target \"{0}\".", targetKey), ex);
            }
        }
Esempio n. 6
0
        public void CreateVersion(Version version)
        {
            if (version == null)
                throw new ArgumentNullException("version", "Version cannot be null.");
            if (version.AppKey == Guid.Empty)
                throw new ArgumentException("App key cannot be empty.", "version.AppKey");

            try
            {
                using (var client = new AmazonS3Client(Context.AwsAccessKeyId, Context.AwsSecretAccessKey))
                {
                    var appsController = new Apps(Context);
                    var app = appsController.GetApp(version.AppKey);
                    version.GroupKey = app.GroupKey;

                    var indexesController = new Internal.Indexes(Context);

                    using (var stream = version.Serialise())
                    {
                        string indexPath = GetAppVersionsIndexPath(version.AppKey);
                        var index = indexesController.LoadIndex(indexPath);
                        if (index.Entries.Any(e => e.Key == version.Key))
                        {
                            throw new DeploymentException("Index already contains entry for given key!");
                        }

                        using (var putResponse = client.PutObject(new PutObjectRequest()
                        {
                            BucketName = Context.BucketName,
                            Key = string.Format("{0}/{1}/{2}", STR_VERSIONS_CONTAINER_PATH, version.Key.ToString("N"), STR_INFO_FILE_NAME),
                            InputStream = stream,
                        })) { }

                        index.Entries.Add(new Internal.EntityIndexEntry() { Key = version.Key, Name = CreateVersionIndexName(version) });
                        Internal.Indexes.NameSortIndex(index, true);
                        indexesController.UpdateIndex(indexPath, index);
                    }
                }
            }
            catch (AmazonS3Exception awsEx)
            {
                throw new DeploymentException("Failed creating version.", awsEx);
            }
            catch (Exception ex)
            {
                throw new DeploymentException("Failed creating version.", ex);
            }
        }
Esempio n. 7
0
        public void CreateTarget(Target target)
        {
            if (target == null)
                throw new ArgumentNullException("target", "Target cannot be null.");
            if (target.GroupKey == Guid.Empty)
                throw new ArgumentException("Group key cannot be empty.", "target.GroupKey");

            using (var stream = target.Serialise())
            {
                try
                {
                    using (var client = new AmazonS3Client(Context.AwsAccessKeyId, Context.AwsSecretAccessKey))
                    {
                        var groupsController = new Groups(Context);
                        if (!groupsController.GroupExists(target.GroupKey))
                            throw new GroupNotFoundException(String.Format("Group with the key \"{0}\" could not be found.", target.GroupKey));

                        var indexesController = new Internal.Indexes(Context);

                        string indexPath = GetGroupTargetsIndexPath(target.GroupKey);
                        var appIndex = indexesController.LoadIndex(indexPath);
                        if (appIndex.Entries.Any(e => e.Key == target.Key))
                        {
                            throw new DeploymentException("Index already contains entry for given key!");
                        }

                        using (var putResponse = client.PutObject(new PutObjectRequest()
                        {
                            BucketName = Context.BucketName,
                            Key = string.Format("{0}/{1}/{2}", STR_TARGETS_CONTAINER_PATH, target.Key.ToString("N"), STR_INFO_FILE_NAME),
                            InputStream = stream,
                        })) { }

                        appIndex.Entries.Add(new Internal.EntityIndexEntry() { Key = target.Key, Name = target.Name });
                        Internal.Indexes.NameSortIndex(appIndex);
                        indexesController.UpdateIndex(indexPath, appIndex);
                    }
                }
                catch (AmazonS3Exception awsEx)
                {
                    throw new DeploymentException("Failed creating target.", awsEx);
                }
            }
        }
Esempio n. 8
0
        public void CreateInstance(Instance instance)
        {
            if (instance == null)
                throw new ArgumentNullException("instance", "Instance cannot be null.");
            if (instance.TargetKey == Guid.Empty)
                throw new ArgumentException("Target key cannot be empty.", "instance.TargetKey");

            using (var stream = instance.Serialise())
            {
                try
                {
                    using (var client = new AmazonS3Client(Context.AwsAccessKeyId, Context.AwsSecretAccessKey))
                    {
                        var targetsController = new Targets(Context);
                        if (!targetsController.TargetExists(instance.TargetKey))
                            throw new TargetNotFoundException(String.Format("Target with the key \"{0}\" could not be found.", instance.TargetKey));

                        var indexesController = new Internal.Indexes(Context);

                        string indexPath = GetTargetInstancesIndexPath(instance.TargetKey);
                        var instanceIndex = indexesController.LoadIndex(indexPath);
                        if (instanceIndex.Entries.Any(e => e.Key == instance.Key))
                        {
                            throw new DeploymentException("Target instances index already contains entry for new instance key!");
                        }

                        using (var putResponse = client.PutObject(new PutObjectRequest()
                        {
                            BucketName = Context.BucketName,
                            Key = string.Format("{0}/{1}/{2}", STR_INSTANCES_CONTAINER_PATH, instance.Key.ToString("N"), STR_INFO_FILE_NAME),
                            InputStream = stream,
                        })) { }

                        instanceIndex.Entries.Add(new Internal.EntityIndexEntry() { Key = instance.Key, Name = instance.Name });
                        Internal.Indexes.NameSortIndex(instanceIndex);
                        indexesController.UpdateIndex(indexPath, instanceIndex);
                    }
                }
                catch (AmazonS3Exception awsEx)
                {
                    throw new DeploymentException("Failed creating instance.", awsEx);
                }
            }
        }
Esempio n. 9
0
        public void CreateGroup(Group group)
        {
            if (group == null)
                throw new ArgumentNullException("group", "Group cannot be null.");

            using (var stream = group.Serialise())
            {
                try
                {
                    using (var client = new AmazonS3Client(Context.AwsAccessKeyId, Context.AwsSecretAccessKey))
                    {
                        var indexesController = new Internal.Indexes(Context);
                        var index = indexesController.LoadIndex(STR_GROUP_INDEX_PATH);
                        if (index.Entries.Any(e => e.Key == group.Key))
                        {
                            throw new DeploymentException("Index already contains entry for given key!");
                        }

                        using (var putResponse = client.PutObject(new PutObjectRequest()
                        {
                            BucketName = Context.BucketName,
                            Key = string.Format("{0}/{1}/{2}", STR_GROUPS_CONTAINER_PATH, group.Key.ToString("N"), STR_INFO_FILE_NAME),
                            InputStream = stream,
                        })) { }

                        index.Entries.Add(new Internal.EntityIndexEntry() { Key = group.Key, Name = group.Name });
                        Internal.Indexes.NameSortIndex(index);
                        indexesController.UpdateIndex(STR_GROUP_INDEX_PATH, index);
                    }
                }
                catch (AmazonS3Exception awsEx)
                {
                    throw new DeploymentException("Failed creating group.", awsEx);
                }
            }
        }
Esempio n. 10
0
        public void UpdateInstance(Instance updatedInstance)
        {
            if (updatedInstance == null)
                throw new ArgumentNullException("updatedInstance", "Instance cannot be null.");

            var existingInstance = GetInstance(updatedInstance.Key);
            // Don't allow moving between targets.
            updatedInstance.TargetKey = existingInstance.TargetKey;

            using (var stream = updatedInstance.Serialise())
            {
                try
                {
                    using (var client = new AmazonS3Client(Context.AwsAccessKeyId, Context.AwsSecretAccessKey))
                    {
                        using (var putResponse = client.PutObject(new PutObjectRequest()
                        {
                            BucketName = Context.BucketName,
                            Key = string.Format("{0}/{1}/{2}", STR_INSTANCES_CONTAINER_PATH, updatedInstance.Key.ToString("N"), STR_INFO_FILE_NAME),
                            InputStream = stream,
                        })) { }

                        var indexesController = new Internal.Indexes(Context);
                        string indexPath = GetTargetInstancesIndexPath(updatedInstance.TargetKey);
                        indexesController.PutIndexEntry(indexPath, new Internal.EntityIndexEntry() { Key = updatedInstance.Key, Name = updatedInstance.Name });
                    }
                }
                catch (AmazonS3Exception awsEx)
                {
                    throw new DeploymentException("Failed updating instance.", awsEx);
                }
            }
        }
Esempio n. 11
0
        public InstanceList SearchInstances(Guid targetKey, string query = null, int offset = 0, int pageSize = 50)
        {
            if (offset < 0)
                throw new ArgumentOutOfRangeException("offset", "Offset cannot be a negative number.");
            if (pageSize < 1)
                throw new ArgumentOutOfRangeException("pageSize", "Page size cannot be less than 1.");
            if (pageSize > 100)
                throw new ArgumentOutOfRangeException("pageSize", "Page size cannot be greater than 100.");

            try
            {
                var indexesController = new Internal.Indexes(Context);
                var index = indexesController.LoadIndex(GetTargetInstancesIndexPath(targetKey));

                var filteredIndex = index.Entries.AsQueryable();

                if (!string.IsNullOrWhiteSpace(query))
                {
                    var queryParts = query.ToLower().Split(new char[] { ' ', '\t', ',' }).Where(qp => !string.IsNullOrWhiteSpace(qp)).ToArray();
                    filteredIndex = filteredIndex.Where(e => queryParts.Any(q => e.Name.ToLower().Contains(q)));
                }

                var count = filteredIndex.Count();
                var listItems = filteredIndex.Skip(offset).Take(pageSize).Select(e => new InstanceListItem() { Key = e.Key, Name = e.Name }).ToList();
                var list = new InstanceList()
                {
                    TargetKey = targetKey,
                    Instances = listItems,
                    Query = query,
                    Offset = offset,
                    PageSize = pageSize,
                    TotalCount = count,
                };

                return list;
            }
            catch (AmazonS3Exception awsEx)
            {
                throw new DeploymentException("Failed searcing instances.", awsEx);
            }
        }
Esempio n. 12
0
        public void UpdateTarget(Target target)
        {
            if (target == null)
                throw new ArgumentNullException("target", "Target cannot be null.");

            var existingTarget = GetTarget(target.Key);
            // Don't allow moving between groups.
            target.GroupKey = existingTarget.GroupKey;

            using (var stream = target.Serialise())
            {
                try
                {
                    using (var client = new AmazonS3Client(Context.AwsAccessKeyId, Context.AwsSecretAccessKey))
                    {
                        using (var putResponse = client.PutObject(new PutObjectRequest()
                        {
                            BucketName = Context.BucketName,
                            Key = string.Format("{0}/{1}/{2}", STR_TARGETS_CONTAINER_PATH, target.Key.ToString("N"), STR_INFO_FILE_NAME),
                            InputStream = stream,
                        })) { }

                        var indexesController = new Internal.Indexes(Context);
                        string indexPath = GetGroupTargetsIndexPath(target.GroupKey);
                        indexesController.PutIndexEntry(indexPath, new Internal.EntityIndexEntry() { Key = target.Key, Name = target.Name });
                    }
                }
                catch (AmazonS3Exception awsEx)
                {
                    throw new DeploymentException("Failed updating target.", awsEx);
                }
            }
        }
Esempio n. 13
0
        public void UpdateVersion(Version version)
        {
            if (version == null)
                throw new ArgumentNullException("version", "Version cannot be null.");
            if (version.Key == Guid.Empty)
                throw new ArgumentException("Version key cannot be empty.", "version.Key");
            // Disabled these checks as we automatically resolve them for now.
            //if (version.AppKey == Guid.Empty)
            //    throw new ArgumentException("Version app key cannot be empty.", "version.AppKey");
            //if (version.GroupKey == Guid.Empty)
            //    throw new ArgumentException("Version group key cannot be empty.", "version.GroupKey");

            var existingVersion = GetVersion(version.Key);
            // Do not allow moving between apps & groups.
            version.AppKey = existingVersion.AppKey;
            version.GroupKey = existingVersion.GroupKey;

            using (var stream = version.Serialise())
            {
                try
                {
                    using (var client = new AmazonS3Client(Context.AwsAccessKeyId, Context.AwsSecretAccessKey))
                    {
                        var indexesController = new Internal.Indexes(Context);

                        using (var putResponse = client.PutObject(new PutObjectRequest()
                        {
                            BucketName = Context.BucketName,
                            Key = string.Format("{0}/{1}/{2}", STR_VERSIONS_CONTAINER_PATH, version.Key.ToString("N"), STR_INFO_FILE_NAME),
                            InputStream = stream,
                        })) { }

                        string indexPath = GetAppVersionsIndexPath(version.AppKey);
                        indexesController.PutIndexEntry(indexPath, new Internal.EntityIndexEntry() { Key = version.Key, Name = CreateVersionIndexName(version) }, true);
                    }
                }
                catch (AmazonS3Exception awsEx)
                {
                    throw new DeploymentException("Failed deleting version.", awsEx);
                }
                catch (Exception ex)
                {
                    throw new DeploymentException("Failed deleting version.", ex);
                }
            }
        }
Esempio n. 14
0
        public void DeleteApp(Guid key)
        {
            var app = GetApp(key);
            try
            {
                Plywood.Internal.AwsHelpers.SoftDeleteFolders(Context, string.Format("{0}/{1}", STR_APPS_CONTAINER_PATH, key.ToString("N")));

                var indexesController = new Internal.Indexes(Context);

                string indexPath = GetGroupAppsIndexPath(app.GroupKey);
                var appIndex = indexesController.LoadIndex(indexPath);
                if (appIndex.Entries.Any(e => e.Key == key))
                {
                    appIndex.Entries.Remove(appIndex.Entries.Single(e => e.Key == key));
                    Internal.Indexes.NameSortIndex(appIndex);
                    indexesController.UpdateIndex(indexPath, appIndex);
                }
            }
            catch (AmazonS3Exception awsEx)
            {
                throw new DeploymentException("Failed deleting app.", awsEx);
            }
        }
Esempio n. 15
0
        public void UpdateApp(App app)
        {
            if (app == null)
                throw new ArgumentNullException("app", "App cannot be null.");

            var existingApp = GetApp(app.Key);
            // Don't allow moving between groups right now as would have to recursively update references from versions and targets within app.
            app.GroupKey = existingApp.GroupKey;

            using (var stream = app.Serialise())
            {
                try
                {
                    using (var client = new AmazonS3Client(Context.AwsAccessKeyId, Context.AwsSecretAccessKey))
                    {
                        var indexesController = new Internal.Indexes(Context);
                        // This will not currently get called.
                        if (existingApp.GroupKey != app.GroupKey)
                        {
                            var groupsController = new Groups(Context);
                            if (!groupsController.GroupExists(app.GroupKey))
                                throw new GroupNotFoundException(string.Format("Group with key \"{0}\" to move app into cannot be found.", app.GroupKey));
                        }
                        using (var putResponse = client.PutObject(new PutObjectRequest()
                        {
                            BucketName = Context.BucketName,
                            Key = string.Format("{0}/{1}/{2}", STR_APPS_CONTAINER_PATH, app.Key.ToString("N"), STR_INFO_FILE_NAME),
                            InputStream = stream,
                        })) { }

                        // This will not currently get called.
                        if (existingApp.GroupKey != app.GroupKey)
                        {
                            string oldAppIndexPath = GetGroupAppsIndexPath(existingApp.GroupKey);
                            indexesController.DeleteIndexEntry(oldAppIndexPath, app.Key);
                        }

                        string newAppIndexPath = GetGroupAppsIndexPath(app.GroupKey);
                        indexesController.PutIndexEntry(newAppIndexPath, new Internal.EntityIndexEntry() { Key = app.Key, Name = app.Name });
                    }
                }
                catch (AmazonS3Exception awsEx)
                {
                    throw new DeploymentException("Failed updating app.", awsEx);
                }
            }
        }
Esempio n. 16
0
        public void DeleteVersion(Guid key)
        {
            var version = GetVersion(key);
            try
            {
                Plywood.Internal.AwsHelpers.SoftDeleteFolders(Context, string.Format("{0}/{1}", STR_VERSIONS_CONTAINER_PATH, key.ToString("N")));

                var indexesController = new Internal.Indexes(Context);

                string indexPath = GetAppVersionsIndexPath(version.AppKey);
                var appIndex = indexesController.LoadIndex(indexPath);
                if (appIndex.Entries.Any(e => e.Key == key))
                {
                    appIndex.Entries.Remove(appIndex.Entries.Single(e => e.Key == key));
                    Internal.Indexes.NameSortIndex(appIndex, true);
                    indexesController.UpdateIndex(indexPath, appIndex);
                }
            }
            catch (AmazonS3Exception awsEx)
            {
                throw new DeploymentException(string.Format("Failed deleting version with key \"{0}\"", key), awsEx);
            }
            catch (Exception ex)
            {
                throw new DeploymentException(string.Format("Failed deleting version with key \"{0}\"", key), ex);
            }
        }
Esempio n. 17
0
        public void DeleteInstance(Guid instanceKey)
        {
            var instance = GetInstance(instanceKey);
            try
            {
                Plywood.Internal.AwsHelpers.SoftDeleteFolders(Context, string.Format("{0}/{1}", STR_INSTANCES_CONTAINER_PATH, instanceKey.ToString("N")));

                var indexesController = new Internal.Indexes(Context);

                string indexPath = GetTargetInstancesIndexPath(instance.TargetKey);
                var appIndex = indexesController.LoadIndex(indexPath);
                if (appIndex.Entries.Any(e => e.Key == instanceKey))
                {
                    appIndex.Entries.Remove(appIndex.Entries.Single(e => e.Key == instanceKey));
                    Internal.Indexes.NameSortIndex(appIndex);
                    indexesController.UpdateIndex(indexPath, appIndex);
                }
            }
            catch (AmazonS3Exception awsEx)
            {
                throw new DeploymentException("Failed deleting instance.", awsEx);
            }
        }
Esempio n. 18
0
        public VersionList SearchAppVersions(Guid appKey, DateTime? fromDate = null, DateTime? toDate = null, string query = null, int offset = 0, int pageSize = 50)
        {
            if (offset < 0)
                throw new ArgumentOutOfRangeException("offset", "Offset cannot be a negative number.");
            if (pageSize < 1)
                throw new ArgumentOutOfRangeException("pageSize", "Page size cannot be less than 1.");
            if (pageSize > 100)
                throw new ArgumentOutOfRangeException("pageSize", "Page size cannot be greater than 100.");

            try
            {
                var indexesController = new Internal.Indexes(Context);
                var index = indexesController.LoadIndex(GetAppVersionsIndexPath(appKey));

                var filteredIndex = index.Entries.AsQueryable();

                if (fromDate != null)
                {
                    filteredIndex = filteredIndex.Where(e => DateTime.Parse(e.Name.Substring(0, e.Name.IndexOf(' '))) >= fromDate);
                }
                if (toDate != null)
                {
                    filteredIndex = filteredIndex.Where(e => DateTime.Parse(e.Name.Substring(0, e.Name.IndexOf(' '))) <= toDate);
                }

                if (!string.IsNullOrWhiteSpace(query))
                {
                    var queryParts = query.ToLower().Split(new char[] { ' ', '\t', ',' }).Where(qp => !string.IsNullOrWhiteSpace(qp)).ToArray();
                    filteredIndex = filteredIndex.Where(e => queryParts.Any(q => e.Name.ToLower().Contains(q)));
                }

                var count = filteredIndex.Count();
                var listItems = filteredIndex.Skip(offset).Take(pageSize).Select(e =>
                    new VersionListItem()
                    {
                        Key = e.Key,
                        Timestamp = DateTime.Parse(e.Name.Substring(0, e.Name.IndexOf(' '))),
                        VersionNumber = e.Name.Substring(e.Name.IndexOf(' ') + 1, e.Name.IndexOf(' ', e.Name.IndexOf(' ') + 1) - (e.Name.IndexOf(' ') + 1)),
                        Comment = e.Name.Substring(e.Name.IndexOf(' ', e.Name.IndexOf(' ') + 1) + 1),
                    }).ToList();

                var list = new VersionList()
                {
                    AppKey = appKey,
                    Versions = listItems,
                    Offset = offset,
                    PageSize = pageSize,
                    TotalCount = count,
                };

                return list;
            }
            catch (AmazonS3Exception awsEx)
            {
                throw new DeploymentException("Failed searcing groups.", awsEx);
            }
            catch (Exception ex)
            {
                throw new DeploymentException("Failed searcing groups.", ex);
            }
        }