Esempio n. 1
0
 private void InitializeBucket(Bucket bucket)
 {
     lock (_BucketsLock)
     {
         BucketClient client = new BucketClient(_Settings, _Logging, bucket, _ORM);
         _Buckets.Add(client);
     }
 }
Esempio n. 2
0
        internal bool Remove(Bucket bucket, bool destroy)
        {
            if (bucket == null)
            {
                throw new ArgumentNullException(nameof(bucket));
            }

            bool removed = false;

            if (_Config.BucketExists(bucket.Name))
            {
                BucketClient client = GetClient(bucket.Name);
                if (client != null)
                {
                    lock (_BucketsLock)
                    {
                        List <BucketClient> clients = _Buckets.Where(b => !b.Name.Equals(bucket.Name)).ToList();
                        _Buckets = new List <BucketClient>(clients);
                        client.Dispose();
                        client = null;
                    }
                }

                removed = true;

                _Config.DeleteBucket(bucket.GUID);
            }

            if (removed)
            {
                if (destroy)
                {
                    if (!Destroy(bucket))
                    {
                        _Logging.Warn("BucketManager Remove issues encountered removing data for bucket " + bucket.Name + ", cleanup required");
                    }
                    else
                    {
                        _Logging.Info("BucketManager Remove removed bucket with name " + bucket.Name + " with owner " + bucket.OwnerGUID);
                    }
                }
                else
                {
                    _Logging.Info("BucketManager Remove removed bucket with name " + bucket.Name + " with owner " + bucket.OwnerGUID);
                }

                return(true);
            }
            else
            {
                _Logging.Warn("BucketManager Remove bucket with name " + bucket.Name + " not found");
                return(false);
            }
        }
Esempio n. 3
0
        internal bool Add(Bucket bucket)
        {
            if (bucket == null)
            {
                throw new ArgumentNullException(nameof(bucket));
            }

            bool success = _Config.AddBucket(bucket);

            if (success)
            {
                BucketClient client = new BucketClient(_Settings, _Logging, bucket, _ORM);

                lock (_BucketsLock)
                {
                    _Buckets.Add(client);
                }

                InitializeBucket(bucket);
            }

            return(success);
        }
Esempio n. 4
0
        private void RunSetup()
        {
            #region Variables

            DateTime timestamp = DateTime.Now;
            Settings settings  = new Settings();

            #endregion

            #region Welcome

            Console.WriteLine("");
            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.WriteLine(Environment.NewLine +
                              @"   _           ____  " + Environment.NewLine +
                              @"  | |___ _____|__ /  " + Environment.NewLine +
                              @"  | / -_|_-<_-<|_ \  " + Environment.NewLine +
                              @"  |_\___/__/__/___/  " + Environment.NewLine +
                              @"                     " + Environment.NewLine +
                              Environment.NewLine);

            Console.ResetColor();

            Console.WriteLine("");
            Console.WriteLine("<3 :: Less3 :: S3-Compatible Object Storage");
            Console.WriteLine("");
            //                          1         2         3         4         5         6         7
            //                 12345678901234567890123456789012345678901234567890123456789012345678901234567890
            Console.WriteLine("Thank you for using Less3!  We're putting together a basic system configuration");
            Console.WriteLine("so you can be up and running quickly.  You'll want to modify the system.json");
            Console.WriteLine("file after to ensure a more secure operating environment.");
            Console.WriteLine("");

            #endregion

            #region Temporary-Instances

            LoggingModule logging = new LoggingModule("127.0.0.1", 514);

            #endregion

            #region Database-and-ORM

            //                          1         2         3         4         5         6         7
            //                 12345678901234567890123456789012345678901234567890123456789012345678901234567890
            Console.WriteLine("");
            Console.WriteLine("Less3 requires access to a database and supports Sqlite, Microsoft SQL Server,");
            Console.WriteLine("MySQL, and PostgreSQL.  Please provide access details for your database.  The");
            Console.WriteLine("user account supplied must have the ability to CREATE and DROP tables along");
            Console.WriteLine("with issue queries containing SELECT, INSERT, UPDATE, and DELETE.  Setup will");
            Console.WriteLine("attempt to create tables on your behalf if they dont exist.");
            Console.WriteLine("");

            bool dbSet = false;

            while (!dbSet)
            {
                string userInput = Common.InputString("Database type [sqlite|sqlserver|mysql|postgresql]:", "sqlite", false);
                switch (userInput)
                {
                case "sqlite":
                    settings.Database = new DatabaseSettings(
                        Common.InputString("Filename:", "./less3.db", false)
                        );

                    //                          1         2         3         4         5         6         7
                    //                 12345678901234567890123456789012345678901234567890123456789012345678901234567890
                    Console.WriteLine("");
                    Console.WriteLine("IMPORTANT: Using Sqlite in production is not recommended if deploying within a");
                    Console.WriteLine("containerized environment and the database file is stored within the container.");
                    Console.WriteLine("Store the database file in external storage to ensure persistence.");
                    Console.WriteLine("");
                    dbSet = true;
                    break;

                case "sqlserver":
                    settings.Database = new DatabaseSettings(
                        Common.InputString("Hostname:", "localhost", false),
                        Common.InputInteger("Port:", 1433, true, false),
                        Common.InputString("Username:"******"sa", false),
                        Common.InputString("Password:"******"Instance (for SQLEXPRESS):", null, true),
                        Common.InputString("Database name:", "less3", false)
                        );
                    dbSet = true;
                    break;

                case "mysql":
                    settings.Database = new DatabaseSettings(
                        DbTypes.Mysql,
                        Common.InputString("Hostname:", "localhost", false),
                        Common.InputInteger("Port:", 3306, true, false),
                        Common.InputString("Username:"******"root", false),
                        Common.InputString("Password:"******"Schema name:", "less3", false)
                        );
                    dbSet = true;
                    break;

                case "postgresql":
                    settings.Database = new DatabaseSettings(
                        DbTypes.Postgresql,
                        Common.InputString("Hostname:", "localhost", false),
                        Common.InputInteger("Port:", 5432, true, false),
                        Common.InputString("Username:"******"postgres", false),
                        Common.InputString("Password:"******"Schema name:", "less3", false)
                        );
                    dbSet = true;
                    break;
                }
            }

            if (!Common.WriteFile("system.json", Common.SerializeJson(settings, true), false))
            {
                Common.ExitApplication("setup", "Unable to write system.json", -1);
                return;
            }

            if (!Directory.Exists(settings.Storage.DiskDirectory))
            {
                Directory.CreateDirectory(settings.Storage.DiskDirectory);
            }

            if (!Directory.Exists(settings.Storage.TempDirectory))
            {
                Directory.CreateDirectory(settings.Storage.TempDirectory);
            }

            #endregion

            #region Create-Configuration-Database

            Watson.ORM.WatsonORM orm = new Watson.ORM.WatsonORM(settings.Database);

            orm.InitializeDatabase();

            orm.InitializeTable(typeof(Bucket));
            orm.InitializeTable(typeof(BucketAcl));
            orm.InitializeTable(typeof(BucketTag));
            orm.InitializeTable(typeof(Credential));
            orm.InitializeTable(typeof(Obj));
            orm.InitializeTable(typeof(ObjectAcl));
            orm.InitializeTable(typeof(ObjectTag));
            orm.InitializeTable(typeof(User));

            ConfigManager config = new ConfigManager(settings, logging, orm);

            string userGuid = "default";
            config.AddUser(new User(userGuid, "Default user", "*****@*****.**"));
            config.AddCredential(userGuid, "My first access key", "default", "default", false);

            Bucket bucketConfig = new Bucket(
                "default",
                userGuid,
                userGuid,
                StorageDriverType.Disk,
                settings.Storage.DiskDirectory + "default/Objects/");
            bucketConfig.EnablePublicRead  = true;
            bucketConfig.EnablePublicWrite = false;
            bucketConfig.EnableVersioning  = false;

            config.AddBucket(bucketConfig);

            #endregion

            #region Write-Sample-Objects

            BucketClient bucket = new BucketClient(settings, logging, bucketConfig, orm);

            DateTime ts = DateTime.Now.ToUniversalTime();

            string htmlFile = SampleHtmlFile("http://github.com/jchristn/less3");
            string textFile = SampleTextFile("http://github.com/jchristn/less3");
            string jsonFile = SampleJsonFile("http://github.com/jchristn/less3");

            Obj obj1 = new Obj();
            obj1.OwnerGUID     = "default";
            obj1.AuthorGUID    = "default";
            obj1.BlobFilename  = Guid.NewGuid().ToString();
            obj1.ContentLength = htmlFile.Length;
            obj1.ContentType   = "text/html";
            obj1.Key           = "hello.html";
            obj1.Md5           = Common.BytesToHexString(Common.Md5(Encoding.UTF8.GetBytes(htmlFile)));
            obj1.Version       = 1;
            obj1.IsFolder      = false;
            obj1.DeleteMarker  = false;
            obj1.CreatedUtc    = ts;
            obj1.LastUpdateUtc = ts;
            obj1.LastAccessUtc = ts;

            Obj obj2 = new Obj();
            obj2.OwnerGUID     = "default";
            obj2.AuthorGUID    = "default";
            obj2.BlobFilename  = Guid.NewGuid().ToString();
            obj2.ContentLength = htmlFile.Length;
            obj2.ContentType   = "text/plain";
            obj2.Key           = "hello.txt";
            obj2.Md5           = Common.BytesToHexString(Common.Md5(Encoding.UTF8.GetBytes(textFile)));
            obj2.Version       = 1;
            obj2.IsFolder      = false;
            obj2.DeleteMarker  = false;
            obj2.CreatedUtc    = ts;
            obj2.LastUpdateUtc = ts;
            obj2.LastAccessUtc = ts;

            Obj obj3 = new Obj();
            obj3.OwnerGUID     = "default";
            obj3.AuthorGUID    = "default";
            obj3.BlobFilename  = Guid.NewGuid().ToString();
            obj3.ContentLength = htmlFile.Length;
            obj3.ContentType   = "application/json";
            obj3.Key           = "hello.json";
            obj3.Md5           = Common.BytesToHexString(Common.Md5(Encoding.UTF8.GetBytes(jsonFile)));
            obj3.Version       = 1;
            obj3.IsFolder      = false;
            obj3.DeleteMarker  = false;
            obj3.CreatedUtc    = ts;
            obj3.LastUpdateUtc = ts;
            obj3.LastAccessUtc = ts;

            bucket.AddObject(obj1, Encoding.UTF8.GetBytes(htmlFile));
            bucket.AddObject(obj2, Encoding.UTF8.GetBytes(textFile));
            bucket.AddObject(obj3, Encoding.UTF8.GetBytes(jsonFile));

            Common.WriteFile("./system.json", Encoding.UTF8.GetBytes(Common.SerializeJson(settings, true)));

            #endregion

            #region Wrap-Up

            //                          1         2         3         4         5         6         7
            //                 12345678901234567890123456789012345678901234567890123456789012345678901234567890
            Console.WriteLine("");
            Console.WriteLine("All finished!");
            Console.WriteLine("");
            Console.WriteLine("If you ever want to return to this setup wizard, just re-run the application");
            Console.WriteLine("from the terminal with the 'setup' argument.");
            Console.WriteLine("");
            Console.WriteLine("We created a bucket containing a few sample files for you so that you can see");
            Console.WriteLine("your node in action.  Access these files in the 'default' bucket using the");
            Console.WriteLine("AWS SDK or your favorite S3 browser tool.");
            Console.WriteLine("");
            Console.WriteLine("  http://" + settings.Server.DnsHostname + ":" + settings.Server.ListenerPort + "/default/hello.html");
            Console.WriteLine("  http://" + settings.Server.DnsHostname + ":" + settings.Server.ListenerPort + "/default/hello.txt");
            Console.WriteLine("  http://" + settings.Server.DnsHostname + ":" + settings.Server.ListenerPort + "/default/hello.json");
            Console.WriteLine("");
            Console.WriteLine("  Access key  : default");
            Console.WriteLine("  Secret key  : default");
            Console.WriteLine("  Bucket name : default (public read enabled!)");
            Console.WriteLine("  S3 endpoint : http://" + settings.Server.DnsHostname + ":" + settings.Server.ListenerPort);
            Console.WriteLine("");
            Console.WriteLine("IMPORTANT: be sure to supply a hostname in the system.json Server.DnsHostname");
            Console.WriteLine("field if you wish to allow access from other machines.  Your node is currently");
            Console.WriteLine("only accessible via localhost.  Do not use an IP address for this value.");
            Console.WriteLine("");

            #endregion
        }